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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
/** @file
A brief file description
@section license License
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 "ProxyConfig.h"
#include "P_EventSystem.h"
#if TS_HAS_TESTS
#include "tscore/TestBox.h"
#endif
ConfigProcessor configProcessor;
class ConfigInfoReleaser : public Continuation
{
public:
ConfigInfoReleaser(unsigned int id, ConfigInfo *info) : Continuation(new_ProxyMutex()), m_id(id), m_info(info)
{
SET_HANDLER(&ConfigInfoReleaser::handle_event);
}
int
handle_event(int /* event ATS_UNUSED */, void * /* edata ATS_UNUSED */)
{
configProcessor.release(m_id, m_info);
delete this;
return EVENT_DONE;
}
public:
unsigned int m_id;
ConfigInfo *m_info;
};
unsigned int
ConfigProcessor::set(unsigned int id, ConfigInfo *info, unsigned timeout_secs)
{
ConfigInfo *old_info;
int idx;
if (id == 0) {
id = ++ninfos;
ink_assert(id != 0);
ink_assert(id <= MAX_CONFIGS);
}
// Don't be an idiot and use a zero timeout ...
ink_assert(timeout_secs > 0);
if (info) {
// New objects *must* start with a zero refcount. The config
// processor holds it's own refcount. We should be the only
// refcount holder at this point.
ink_release_assert(info->refcount_inc() == 1);
}
if (id > MAX_CONFIGS) {
// invalid index
Error("[ConfigProcessor::set] invalid index");
return 0;
}
idx = id - 1;
old_info = infos[idx].exchange(info);
Debug("config", "Set for slot %d 0x%" PRId64 " was 0x%" PRId64 " with ref count %d", id, (int64_t)info, (int64_t)old_info,
(old_info) ? old_info->refcount() : 0);
if (old_info) {
// The ConfigInfoReleaser now takes our refcount, but
// some other thread might also have one ...
ink_assert(old_info->refcount() > 0);
eventProcessor.schedule_in(new ConfigInfoReleaser(id, old_info), HRTIME_SECONDS(timeout_secs));
}
return id;
}
ConfigInfo *
ConfigProcessor::get(unsigned int id)
{
ConfigInfo *info;
int idx;
ink_assert(id <= MAX_CONFIGS);
if (id == 0 || id > MAX_CONFIGS) {
// because of an invalid index
return nullptr;
}
idx = id - 1;
info = infos[idx];
if (info) {
// Hand out a refcount to the caller. We should still have out
// own refcount, so it should be at least 2.
ink_release_assert(info->refcount_inc() > 1);
}
return info;
}
void
ConfigProcessor::release(unsigned int id, ConfigInfo *info)
{
int idx;
if (id == 0 || id > MAX_CONFIGS) {
// nothing to delete since we have an invalid index
ink_abort("released an invalid id '%u'", id);
}
idx = id - 1;
if (info && info->refcount_dec() == 0) {
// When we release, we should already have replaced this object in the index.
Debug("config", "Release config %d 0x%" PRId64, id, (int64_t)info);
ink_release_assert(info != this->infos[idx]);
delete info;
}
}
#if TS_HAS_TESTS
enum {
REGRESSION_CONFIG_FIRST = 1, // last config in a sequence
REGRESSION_CONFIG_LAST = 2, // last config in a sequence
REGRESSION_CONFIG_SINGLE = 4, // single-owner config
};
struct RegressionConfig : public ConfigInfo {
static int nobjects; // count of outstanding RegressionConfig objects (not-reentrant)
// DeferredCall is a simple function call wrapper that defers itself until the RegressionConfig
// object count drops below the specified count.
template <typename CallType> struct DeferredCall : public Continuation {
DeferredCall(int _r, CallType _c) : remain(_r), call(_c) { SET_HANDLER(&DeferredCall::handleEvent); }
int
handleEvent(int event ATS_UNUSED, Event *e)
{
if (RegressionConfig::nobjects > this->remain) {
e->schedule_in(HRTIME_MSECONDS(500));
return EVENT_CONT;
}
call();
delete this;
return EVENT_DONE;
}
int remain; // Number of remaining RegressionConfig objects to wait for.
CallType call;
};
template <typename CallType>
static void
defer(int count, CallType call)
{
eventProcessor.schedule_in(new RegressionConfig::DeferredCall<CallType>(count, call), HRTIME_MSECONDS(500));
}
RegressionConfig(RegressionTest *r, int *ps, unsigned f) : test(r), pstatus(ps), flags(f)
{
if (this->flags & REGRESSION_CONFIG_SINGLE) {
TestBox box(this->test, this->pstatus);
box.check(this->refcount() == 1, "invalid refcount %d (should be 1)", this->refcount());
}
ink_atomic_increment(&nobjects, 1);
}
~RegressionConfig() override
{
TestBox box(this->test, this->pstatus);
box.check(this->refcount() == 0, "invalid refcount %d (should be 0)", this->refcount());
// If we are the last config to be scheduled, pass the test.
// Otherwise, verify that the test is still running ...
if (REGRESSION_CONFIG_LAST & flags) {
*this->pstatus = REGRESSION_TEST_PASSED;
} else {
box.check(*this->pstatus == REGRESSION_TEST_INPROGRESS, "intermediate config out of sequence, *pstatus is %d", *pstatus);
}
ink_atomic_increment(&nobjects, -1);
}
RegressionTest *test;
int *pstatus;
unsigned flags;
};
int RegressionConfig::nobjects = 0;
struct ProxyConfig_Set_Completion {
ProxyConfig_Set_Completion(int _id, RegressionConfig *_c) : configid(_id), config(_c) {}
void
operator()() const
{
// Push one more RegressionConfig to force the LAST-tagged one to get destroyed.
rprintf(config->test, "setting LAST config object %p\n", config);
configProcessor.set(configid, config, 1);
}
int configid;
RegressionConfig *config;
};
// Test that ConfigProcessor::set() correctly releases the old ConfigInfo after a timeout.
EXCLUSIVE_REGRESSION_TEST(ProxyConfig_Set)(RegressionTest *test, int /* atype ATS_UNUSED */, int *pstatus)
{
int configid = 0;
*pstatus = REGRESSION_TEST_INPROGRESS;
RegressionConfig::nobjects = 0;
configid = configProcessor.set(configid, new RegressionConfig(test, pstatus, REGRESSION_CONFIG_FIRST), 1);
configid = configProcessor.set(configid, new RegressionConfig(test, pstatus, REGRESSION_CONFIG_FIRST), 1);
configid = configProcessor.set(configid, new RegressionConfig(test, pstatus, REGRESSION_CONFIG_FIRST), 1);
configid = configProcessor.set(configid, new RegressionConfig(test, pstatus, REGRESSION_CONFIG_FIRST), 1);
configid = configProcessor.set(configid, new RegressionConfig(test, pstatus, REGRESSION_CONFIG_FIRST), 1);
configid = configProcessor.set(configid, new RegressionConfig(test, pstatus, REGRESSION_CONFIG_FIRST), 1);
configid = configProcessor.set(configid, new RegressionConfig(test, pstatus, REGRESSION_CONFIG_LAST), 1);
// Wait until there's only 2 objects remaining, the one in ConfigProcessor, and the one we make here.
RegressionConfig::defer(2, ProxyConfig_Set_Completion(configid, new RegressionConfig(test, pstatus, 0)));
}
struct ProxyConfig_Release_Completion {
ProxyConfig_Release_Completion(int _id, RegressionConfig *_c) : configid(_id), config(_c) {}
void
operator()() const
{
// Release the reference count. Since we were keeping this alive, it should be the last to die.
configProcessor.release(configid, config);
}
int configid;
RegressionConfig *config;
};
// Test that ConfigProcessor::release() correctly releases the old ConfigInfo across an implicit
// release timeout.
EXCLUSIVE_REGRESSION_TEST(ProxyConfig_Release)(RegressionTest *test, int /* atype ATS_UNUSED */, int *pstatus)
{
int configid = 0;
RegressionConfig *config;
*pstatus = REGRESSION_TEST_INPROGRESS;
RegressionConfig::nobjects = 0;
// Set an initial config, then get it back to hold a reference count.
configid = configProcessor.set(configid, new RegressionConfig(test, pstatus, REGRESSION_CONFIG_LAST), 1);
config = (RegressionConfig *)configProcessor.get(configid);
// Now update the config a few times.
configid = configProcessor.set(configid, new RegressionConfig(test, pstatus, REGRESSION_CONFIG_FIRST), 1);
configid = configProcessor.set(configid, new RegressionConfig(test, pstatus, REGRESSION_CONFIG_FIRST), 1);
configid = configProcessor.set(configid, new RegressionConfig(test, pstatus, REGRESSION_CONFIG_FIRST), 1);
configid = configProcessor.set(configid, new RegressionConfig(test, pstatus, 0), 1);
// Defer the release of the object that we held back until there are only 2 left. The one we are holding
// and the one in the ConfigProcessor. Then releasing the one we hold will trigger the LAST check
RegressionConfig::defer(2, ProxyConfig_Release_Completion(configid, config));
}
#endif /* TS_HAS_TESTS */
|