|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package gcpspanner |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "time" |
| 21 | + |
| 22 | + "cloud.google.com/go/spanner" |
| 23 | +) |
| 24 | + |
| 25 | +const savedSearchStateTableName = "SavedSearchState" |
| 26 | + |
| 27 | +type savedSearchStateMapper struct{} |
| 28 | + |
| 29 | +type savedSearchStateKey struct { |
| 30 | + SavedSearchID string |
| 31 | + SnapshotType SavedSearchSnapshotType |
| 32 | +} |
| 33 | + |
| 34 | +type SavedSearchSnapshotType string |
| 35 | + |
| 36 | +const ( |
| 37 | + SavedSearchSnapshotTypeImmediate SavedSearchSnapshotType = "IMMEDIATE" |
| 38 | + SavedSearchSnapshotTypeWeekly SavedSearchSnapshotType = "WEEKLY" |
| 39 | + SavedSearchSnapshotTypeMonthly SavedSearchSnapshotType = "MONTHLY" |
| 40 | +) |
| 41 | + |
| 42 | +type SavedSearchState struct { |
| 43 | + SavedSearchID string `spanner:"SavedSearchId"` |
| 44 | + SnapshotType SavedSearchSnapshotType `spanner:"SnapshotType"` |
| 45 | + LastKnownStateBlobPath *string `spanner:"LastKnownStateBlobPath"` |
| 46 | + WorkerLockID *string `spanner:"WorkerLockId"` |
| 47 | + WorkerLockExpiresAt *time.Time `spanner:"WorkerLockExpiresAt"` |
| 48 | +} |
| 49 | + |
| 50 | +func (m savedSearchStateMapper) SelectOne(key savedSearchStateKey) spanner.Statement { |
| 51 | + return spanner.Statement{ |
| 52 | + SQL: "SELECT * FROM SavedSearchState WHERE SavedSearchId = @SavedSearchId AND SnapshotType = @SnapshotType", |
| 53 | + Params: map[string]any{"SavedSearchId": key.SavedSearchID, "SnapshotType": key.SnapshotType}, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +var ( |
| 58 | + ErrAlreadyLocked = errors.New("resource already locked by another worker") |
| 59 | + ErrLockNotOwned = errors.New("cannot release lock not owned by worker") |
| 60 | +) |
| 61 | + |
| 62 | +// TryAcquireSavedSearchStateWorkerLock attempts to acquire a worker lock for the given saved search and snapshot type. |
| 63 | +// If the lock is already held by another worker and is still active, ErrAlreadyLocked is returned. |
| 64 | +// A caller can re-acquire a lock it already holds it (thereby extending the expiration). |
| 65 | +func (c *Client) TryAcquireSavedSearchStateWorkerLock( |
| 66 | + ctx context.Context, |
| 67 | + savedSearchID string, |
| 68 | + snapshotType SavedSearchSnapshotType, |
| 69 | + workerID string, |
| 70 | + ttl time.Duration) (bool, error) { |
| 71 | + writer := newEntityMutator[savedSearchStateMapper, SavedSearchState](c) |
| 72 | + key := savedSearchStateKey{SavedSearchID: savedSearchID, SnapshotType: snapshotType} |
| 73 | + |
| 74 | + err := writer.readInspectMutate(ctx, key, |
| 75 | + func(_ context.Context, existing *SavedSearchState) (*spanner.Mutation, error) { |
| 76 | + now := c.timeNow() |
| 77 | + |
| 78 | + // If row exists, is it locked by someone else not the caller? |
| 79 | + if existing != nil { |
| 80 | + isLocked := existing.WorkerLockID != nil && *existing.WorkerLockID != workerID |
| 81 | + isActive := existing.WorkerLockExpiresAt != nil && existing.WorkerLockExpiresAt.After(now) |
| 82 | + |
| 83 | + if isLocked && isActive { |
| 84 | + return nil, ErrAlreadyLocked |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + expiration := now.Add(ttl) |
| 89 | + |
| 90 | + // We can take the lock. |
| 91 | + newState := SavedSearchState{ |
| 92 | + SavedSearchID: savedSearchID, |
| 93 | + SnapshotType: snapshotType, |
| 94 | + WorkerLockID: &workerID, |
| 95 | + WorkerLockExpiresAt: &expiration, |
| 96 | + LastKnownStateBlobPath: nil, |
| 97 | + } |
| 98 | + if existing != nil { |
| 99 | + newState.LastKnownStateBlobPath = existing.LastKnownStateBlobPath |
| 100 | + } |
| 101 | + |
| 102 | + return spanner.InsertOrUpdateStruct(savedSearchStateTableName, newState) |
| 103 | + }) |
| 104 | + |
| 105 | + if err != nil { |
| 106 | + return false, err |
| 107 | + } |
| 108 | + |
| 109 | + return true, nil |
| 110 | +} |
| 111 | + |
| 112 | +// ReleaseSavedSearchStateWorkerLock releases the worker lock for the given saved search and snapshot type. |
| 113 | +// The caller must own the lock. If not, ErrLockNotOwned is returned. |
| 114 | +func (c *Client) ReleaseSavedSearchStateWorkerLock( |
| 115 | + ctx context.Context, |
| 116 | + savedSearchID string, |
| 117 | + snapshotType SavedSearchSnapshotType, |
| 118 | + workerID string) error { |
| 119 | + mutator := newEntityMutator[savedSearchStateMapper, SavedSearchState](c) |
| 120 | + key := savedSearchStateKey{SavedSearchID: savedSearchID, SnapshotType: snapshotType} |
| 121 | + |
| 122 | + return mutator.readInspectMutate(ctx, key, |
| 123 | + func(_ context.Context, existing *SavedSearchState) (*spanner.Mutation, error) { |
| 124 | + // If row is gone, nothing to release |
| 125 | + if existing == nil { |
| 126 | + return nil, nil |
| 127 | + } |
| 128 | + |
| 129 | + // Verify the caller owns this lock |
| 130 | + if existing.WorkerLockID == nil || *existing.WorkerLockID != workerID { |
| 131 | + return nil, ErrLockNotOwned |
| 132 | + } |
| 133 | + |
| 134 | + newState := SavedSearchState{ |
| 135 | + SavedSearchID: savedSearchID, |
| 136 | + SnapshotType: snapshotType, |
| 137 | + // Release the lock |
| 138 | + WorkerLockID: nil, |
| 139 | + WorkerLockExpiresAt: nil, |
| 140 | + LastKnownStateBlobPath: nil, |
| 141 | + } |
| 142 | + |
| 143 | + // Preserve the existing blob path |
| 144 | + newState.LastKnownStateBlobPath = existing.LastKnownStateBlobPath |
| 145 | + |
| 146 | + return spanner.InsertOrUpdateStruct(savedSearchStateTableName, newState) |
| 147 | + }) |
| 148 | +} |
| 149 | + |
| 150 | +// GetSavedSearchState retrieves the SavedSearchState for the given saved search and snapshot type. |
| 151 | +// If no such row exists, ErrQueryReturnedNoResults is returned. |
| 152 | +func (c *Client) GetSavedSearchState( |
| 153 | + ctx context.Context, |
| 154 | + savedSearchID string, |
| 155 | + snapshotType SavedSearchSnapshotType) (*SavedSearchState, error) { |
| 156 | + r := newEntityReader[savedSearchStateMapper, SavedSearchState, savedSearchStateKey](c) |
| 157 | + key := savedSearchStateKey{SavedSearchID: savedSearchID, SnapshotType: snapshotType} |
| 158 | + |
| 159 | + return r.readRowByKey(ctx, key) |
| 160 | +} |
| 161 | + |
| 162 | +// UpdateSavedSearchStateLastKnownStateBlobPath updates the LastKnownStateBlobPath |
| 163 | +// for the given saved search and snapshot type. |
| 164 | +// The row must already exist. Else, ErrQueryReturnedNoResults is returned. |
| 165 | +func (c *Client) UpdateSavedSearchStateLastKnownStateBlobPath( |
| 166 | + ctx context.Context, |
| 167 | + savedSearchID string, |
| 168 | + snapshotType SavedSearchSnapshotType, |
| 169 | + blobPath string) error { |
| 170 | + mutator := newEntityMutator[savedSearchStateMapper, SavedSearchState](c) |
| 171 | + key := savedSearchStateKey{SavedSearchID: savedSearchID, SnapshotType: snapshotType} |
| 172 | + |
| 173 | + return mutator.readInspectMutate(ctx, key, |
| 174 | + func(_ context.Context, existing *SavedSearchState) (*spanner.Mutation, error) { |
| 175 | + if existing == nil { |
| 176 | + return nil, ErrQueryReturnedNoResults |
| 177 | + } |
| 178 | + // Update existing row |
| 179 | + existing.LastKnownStateBlobPath = &blobPath |
| 180 | + |
| 181 | + return spanner.UpdateStruct(savedSearchStateTableName, *existing) |
| 182 | + }) |
| 183 | +} |
0 commit comments