MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / getStatusFromCluster

Method getStatusFromCluster

db/background_mgr.go:285–339  ·  view source on GitHub ↗
(ctx context.Context)

Source from the content-addressed store, hash-verified

283}
284
285func (b *BackgroundManager) getStatusFromCluster(ctx context.Context) ([]byte, error) {
286 status, statusCas, err := b.clusterAwareOptions.metadataStore.GetSubDocRaw(ctx, b.clusterAwareOptions.StatusDocID(), "status")
287 if err != nil {
288 if base.IsDocNotFoundError(err) {
289 return nil, nil
290 }
291 return nil, err
292 }
293
294 var clusterStatus map[string]interface{}
295 err = base.JSONUnmarshal(status, &clusterStatus)
296 if err != nil {
297 return nil, err
298 }
299
300 // Work here is required because if the process crashes we'd end up in a state where a GET would return 'running'
301 // when in-fact it crashed.
302 // Worst case we should do this once if we have to do this and update the cluster status doc
303 if clusterState, ok := clusterStatus["status"].(string); ok &&
304 clusterState != string(BackgroundProcessStateCompleted) &&
305 clusterState != string(BackgroundProcessStateStopped) &&
306 clusterState != string(BackgroundProcessStateError) {
307 _, _, err = b.clusterAwareOptions.metadataStore.GetRaw(b.clusterAwareOptions.HeartbeatDocID())
308 if err != nil {
309 if base.IsDocNotFoundError(err) {
310 if clusterState == string(BackgroundProcessStateRunning) {
311 status, _, err = b.getStatusLocal()
312 if err != nil {
313 return nil, err
314 }
315 } else {
316 clusterStatus["status"] = BackgroundProcessStateStopped
317 status, err = base.JSONMarshal(clusterStatus)
318 if err != nil {
319 return nil, err
320 }
321 }
322
323 // In the event there is a crash and need to update the status we should attempt to update the doc to
324 // avoid this unmarshal / marshal work from having to happen again, next time GET is called.
325 // If there is an error we can just ignore it as worst case we run this unmarshal / marshal again on
326 // next request
327 _, err = b.clusterAwareOptions.metadataStore.WriteSubDoc(ctx, b.clusterAwareOptions.StatusDocID(), "status", statusCas, status)
328 if err != nil {
329 status, _, err = b.clusterAwareOptions.metadataStore.GetSubDocRaw(ctx, b.clusterAwareOptions.StatusDocID(), "status")
330 if err != nil {
331 return nil, err
332 }
333 }
334 }
335 }
336 }
337
338 return status, err
339}
340
341func (b *BackgroundManager) resetStatus() {
342 b.lock.Lock()

Callers 1

GetStatusMethod · 0.95

Calls 9

getStatusLocalMethod · 0.95
IsDocNotFoundErrorFunction · 0.92
JSONUnmarshalFunction · 0.92
JSONMarshalFunction · 0.92
StatusDocIDMethod · 0.80
HeartbeatDocIDMethod · 0.80
GetSubDocRawMethod · 0.45
GetRawMethod · 0.45
WriteSubDocMethod · 0.45

Tested by

no test coverage detected