// UnlockSrvShard unlocks a previously locked serving shard. func (n *ActionNode) UnlockSrvShard(ctx context.Context, ts topo.Server, cell, keyspace, shard string, lockPath string, actionError error) error { span := trace.NewSpanFromContext(ctx) span.StartClient("TopoServer.UnlockSrvShardForAction") span.Annotate("action", n.Action) span.Annotate("keyspace", keyspace) span.Annotate("shard", shard) span.Annotate("cell", cell) defer span.Finish() // first update the actionNode if actionError != nil { log.Infof("Unlocking serving shard %v/%v/%v for action %v with error %v", cell, keyspace, shard, n.Action, actionError) n.Error = actionError.Error() n.State = ACTION_STATE_FAILED } else { log.Infof("Unlocking serving shard %v/%v/%v for successful action %v", cell, keyspace, shard, n.Action) n.Error = "" n.State = ACTION_STATE_DONE } err := ts.UnlockSrvShardForAction(cell, keyspace, shard, lockPath, n.ToJson()) if actionError != nil { if err != nil { // this will be masked log.Warningf("UnlockSrvShardForAction failed: %v", err) } return actionError } return err }
func CheckSrvShardLock(t *testing.T, ts topo.Server) { // make sure we can create the lock even if no directory exists interrupted := make(chan struct{}, 1) lockPath, err := ts.LockSrvShardForAction("test", "test_keyspace", "10-20", "fake-content", 5*time.Second, interrupted) if err != nil { t.Fatalf("LockSrvShardForAction: %v", err) } if err := ts.UnlockSrvShardForAction("test", "test_keyspace", "10-20", lockPath, "fake-results"); err != nil { t.Errorf("UnlockShardForAction(): %v", err) } // now take the lock again after the root exists lockPath, err = ts.LockSrvShardForAction("test", "test_keyspace", "10-20", "fake-content", 5*time.Second, interrupted) if err != nil { t.Fatalf("LockSrvShardForAction: %v", err) } // test we can't take the lock again if _, err := ts.LockSrvShardForAction("test", "test_keyspace", "10-20", "unused-fake-content", time.Second/2, interrupted); err != topo.ErrTimeout { t.Errorf("LockSrvShardForAction(again): %v", err) } // test we can interrupt taking the lock go func() { time.Sleep(time.Second / 2) close(interrupted) }() if _, err := ts.LockSrvShardForAction("test", "test_keyspace", "10-20", "unused-fake-content", 5*time.Second, interrupted); err != topo.ErrInterrupted { t.Errorf("LockSrvShardForAction(interrupted): %v", err) } // unlock now if err := ts.UnlockSrvShardForAction("test", "test_keyspace", "10-20", lockPath, "fake-results"); err != nil { t.Errorf("UnlockSrvShardForAction(): %v", err) } // test we can't unlock again if err := ts.UnlockSrvShardForAction("test", "test_keyspace", "10-20", lockPath, "fake-results"); err == nil { t.Error("UnlockSrvShardForAction(again) worked") } }