@@ -138,20 +138,38 @@ void InMemoryRegistry::RunGcTask() {
138
138
service_circuit_breaker_config_data_.CheckGc (min_gc_time);
139
139
}
140
140
141
- Service* InMemoryRegistry::GetOrCreateServiceInLock (const ServiceKey& service_key) {
141
+ Service* InMemoryRegistry::CreateServiceInLock (const ServiceKey& service_key) {
142
142
Service* service = NULL ;
143
143
pthread_rwlock_wrlock (&rwlock_);
144
144
std::map<ServiceKey, Service*>::iterator service_it = service_cache_.find (service_key);
145
- if (service_it == service_cache_.end ()) {
146
- service = new Service (service_key, ++next_service_id_);
147
- service_cache_[service_key] = service;
148
- } else {
145
+ POLARIS_ASSERT (service_it == service_cache_.end ())
146
+ service = new Service (service_key, ++next_service_id_);
147
+ service_cache_[service_key] = service;
148
+ pthread_rwlock_unlock (&rwlock_);
149
+ return service;
150
+ }
151
+
152
+ Service* InMemoryRegistry::GetServiceInLock (const ServiceKey& service_key) {
153
+ Service* service = NULL ;
154
+ pthread_rwlock_wrlock (&rwlock_);
155
+ std::map<ServiceKey, Service*>::iterator service_it = service_cache_.find (service_key);
156
+ if (service_it != service_cache_.end ()) {
149
157
service = service_it->second ;
150
158
}
151
159
pthread_rwlock_unlock (&rwlock_);
152
160
return service;
153
161
}
154
162
163
+ void InMemoryRegistry::DeleteServiceInLock (const ServiceKey& service_key) {
164
+ pthread_rwlock_wrlock (&rwlock_);
165
+ std::map<ServiceKey, Service*>::iterator service_it = service_cache_.find (service_key);
166
+ if (service_it != service_cache_.end ()) {
167
+ delete service_it->second ;
168
+ service_cache_.erase (service_it);
169
+ }
170
+ pthread_rwlock_unlock (&rwlock_);
171
+ }
172
+
155
173
void InMemoryRegistry::CheckExpireServiceData (uint64_t min_access_time,
156
174
RcuMap<ServiceKey, ServiceData>& rcu_cache,
157
175
ServiceDataType service_data_type) {
@@ -166,17 +184,16 @@ void InMemoryRegistry::CheckExpireServiceData(uint64_t min_access_time,
166
184
if (service_data_notify_map_.erase (service_key_with_type) > 0 ) { // 有通知对象表示注册过handler
167
185
context_->GetServerConnector ()->DeregisterEventHandler (expired_services[i],
168
186
service_data_type);
169
- } else { // 没有通知对象,表示未注册过handler,从磁盘加载后从未访问过的数据,直接删除数据
170
- rcu_cache.Delete (expired_services[i]);
171
- context_impl->GetServiceRecord ()->ServiceDataDelete (expired_services[i], service_data_type);
172
- context_impl->GetCacheManager ()->GetCachePersist ().PersistServiceData (expired_services[i],
173
- service_data_type, " " );
174
187
}
175
- pthread_rwlock_unlock (¬ify_rwlock_);
176
188
if (service_data_type == kServiceDataInstances ) { // 清除实例数据时对应的服务级别插件也删除
177
189
context_impl->DeleteServiceContext (expired_services[i]);
178
190
DeleteServiceInLock (expired_services[i]);
179
191
}
192
+ rcu_cache.Delete (expired_services[i]);
193
+ context_impl->GetServiceRecord ()->ServiceDataDelete (expired_services[i], service_data_type);
194
+ context_impl->GetCacheManager ()->GetCachePersist ().PersistServiceData (expired_services[i],
195
+ service_data_type, " " );
196
+ pthread_rwlock_unlock (¬ify_rwlock_);
180
197
}
181
198
}
182
199
@@ -249,6 +266,9 @@ ReturnCode InMemoryRegistry::LoadServiceDataWithNotify(const ServiceKey& service
249
266
if (interval_it != service_interval_map_.end ()) {
250
267
refresh_interval = interval_it->second ;
251
268
}
269
+ if (data_type == kServiceDataInstances ) {
270
+ CreateServiceInLock (service_key);
271
+ }
252
272
// 先加载磁盘缓存数据
253
273
CachePersist& cache_persist = context_->GetContextImpl ()->GetCacheManager ()->GetCachePersist ();
254
274
ServiceData* disk_service_data = cache_persist.LoadServiceData (service_key, data_type);
@@ -269,25 +289,21 @@ ReturnCode InMemoryRegistry::LoadServiceDataWithNotify(const ServiceKey& service
269
289
return kReturnOk ;
270
290
}
271
291
272
- void InMemoryRegistry::DeleteServiceInLock (const ServiceKey& service_key) {
273
- pthread_rwlock_wrlock (&rwlock_);
274
- std::map<ServiceKey, Service*>::iterator service_it = service_cache_.find (service_key);
275
- if (service_it != service_cache_.end ()) {
276
- delete service_it->second ;
277
- service_cache_.erase (service_it);
278
- }
279
- pthread_rwlock_unlock (&rwlock_);
280
- }
281
-
282
292
ReturnCode InMemoryRegistry::UpdateServiceData (const ServiceKey& service_key,
283
293
ServiceDataType data_type,
284
294
ServiceData* service_data) {
285
- if (service_data != NULL ) { // 更新服务数据指向服务
286
- Service* service = GetOrCreateServiceInLock (service_key);
295
+ Service* service = GetServiceInLock (service_key);
296
+ if ( service != NULL ) { // 更新服务数据指向服务
287
297
service->UpdateData (service_data);
288
298
}
289
299
ContextImpl* context_impl = context_->GetContextImpl ();
290
300
if (data_type == kServiceDataInstances ) {
301
+ if (service == NULL ) { // 服务被反注册了
302
+ if (service_data != NULL ) {
303
+ service_data->DecrementRef ();
304
+ }
305
+ return kReturnOk ;
306
+ }
291
307
ServiceData* old_service_data = service_instances_data_.Get (service_key);
292
308
if (old_service_data != NULL ) {
293
309
PluginManager::Instance ().OnPreUpdateServiceData (old_service_data, service_data);
@@ -308,14 +324,6 @@ ReturnCode InMemoryRegistry::UpdateServiceData(const ServiceKey& service_key,
308
324
POLARIS_ASSERT (false );
309
325
}
310
326
if (service_data == NULL ) { // Server Connector反注册Handler触发更新为NULL
311
- if (data_type == kServiceDataInstances ) { // 删除服务实例数据时,同时删除服务
312
- context_impl->DeleteServiceContext (service_key);
313
- DeleteServiceInLock (service_key);
314
- }
315
- context_impl->GetServiceRecord ()->ServiceDataDelete (service_key,
316
- data_type); // 同步记录Service数据删除
317
- context_impl->GetCacheManager ()->GetCachePersist ().PersistServiceData (
318
- service_key, data_type, " " ); // 异步删除磁盘服务数据
319
327
return kReturnOk ;
320
328
}
321
329
context_impl->GetServiceRecord ()->ServiceDataUpdate (service_data); // 同步记录Service版本变化
0 commit comments