summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/qpid/cluster/ExpiryPolicy.cpp
blob: d9a7b0122af1a4436b745df59fcd81e52a97860c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

#include "qpid/broker/Message.h"
#include "qpid/cluster/ExpiryPolicy.h"
#include "qpid/cluster/Multicaster.h"
#include "qpid/framing/ClusterMessageExpiredBody.h"
#include "qpid/sys/Time.h"
#include "qpid/sys/Timer.h"
#include "qpid/log/Statement.h"

namespace qpid {
namespace cluster {

ExpiryPolicy::ExpiryPolicy(Multicaster& m, const MemberId& id, sys::Timer& t)
    : expiryId(1), expiredPolicy(new Expired), mcast(m), memberId(id), timer(t) {}

struct ExpiryTask : public sys::TimerTask {
    ExpiryTask(const boost::intrusive_ptr<ExpiryPolicy>& policy, uint64_t id, sys::AbsTime when)
        : TimerTask(when,"ExpiryPolicy"), expiryPolicy(policy), expiryId(id) {}
    void fire() { expiryPolicy->sendExpire(expiryId); }
    boost::intrusive_ptr<ExpiryPolicy> expiryPolicy;
    const uint64_t expiryId;
};

// Called while receiving an update
void ExpiryPolicy::setId(uint64_t id) {
    sys::Mutex::ScopedLock l(lock);
    expiryId = id;
}

// Called while giving an update
uint64_t ExpiryPolicy::getId() const {
    sys::Mutex::ScopedLock l(lock);
    return expiryId;
}

// Called in enqueuing connection thread
void ExpiryPolicy::willExpire(broker::Message& m) {
    uint64_t id;
    {
        // When messages are fanned out to multiple queues, update sends
        // them as independenty messages so we can have multiple messages
        // with the same expiry ID.
        //
        sys::Mutex::ScopedLock l(lock);
        id = expiryId++;
        if (!id) {              // This is an update of an already-expired message.
            m.setExpiryPolicy(expiredPolicy);
        }
        else {
            assert(unexpiredByMessage.find(&m) == unexpiredByMessage.end());
            // If this is an update, the id may already exist
            unexpiredById.insert(IdMessageMap::value_type(id, &m));
            unexpiredByMessage[&m] = id;
        }
    }
    timer.add(new ExpiryTask(this, id, m.getExpiration()));
}

// Called in dequeueing connection thread
void ExpiryPolicy::forget(broker::Message& m) {
    sys::Mutex::ScopedLock l(lock);
    MessageIdMap::iterator i = unexpiredByMessage.find(&m);
    assert(i != unexpiredByMessage.end());
    unexpiredById.erase(i->second);
    unexpiredByMessage.erase(i);
}

// Called in dequeueing connection or cleanup thread.
bool ExpiryPolicy::hasExpired(broker::Message& m) {
    sys::Mutex::ScopedLock l(lock);
    return unexpiredByMessage.find(&m) == unexpiredByMessage.end();
}

// Called in timer thread
void ExpiryPolicy::sendExpire(uint64_t id) {
    {
        sys::Mutex::ScopedLock l(lock);
        // Don't multicast an expiry notice if message is already forgotten.
        if (unexpiredById.find(id) == unexpiredById.end()) return;
    }
    mcast.mcastControl(framing::ClusterMessageExpiredBody(framing::ProtocolVersion(), id), memberId);
}

// Called in CPG deliver thread.
void ExpiryPolicy::deliverExpire(uint64_t id) {
    sys::Mutex::ScopedLock l(lock);
    std::pair<IdMessageMap::iterator, IdMessageMap::iterator> expired = unexpiredById.equal_range(id);
    IdMessageMap::iterator i = expired.first;
    while (i != expired.second) {
        i->second->setExpiryPolicy(expiredPolicy); // hasExpired() == true; 
        unexpiredByMessage.erase(i->second);
        unexpiredById.erase(i++);
    }
}

// Called in update thread on the updater.
boost::optional<uint64_t> ExpiryPolicy::getId(broker::Message& m) {
    sys::Mutex::ScopedLock l(lock);
    MessageIdMap::iterator i = unexpiredByMessage.find(&m);
    return i == unexpiredByMessage.end() ? boost::optional<uint64_t>() : i->second;
}

bool ExpiryPolicy::Expired::hasExpired(broker::Message&) { return true; }
void ExpiryPolicy::Expired::willExpire(broker::Message&) { }

}} // namespace qpid::cluster