|
| 1 | +package azure |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "net/url" |
| 9 | + "strconv" |
| 10 | + |
| 11 | + "github.com/Azure/azure-storage-blob-go/azblob" |
| 12 | + kedav1alpha1 "github.com/kedacore/keda/v2/api/v1alpha1" |
| 13 | + "github.com/kedacore/keda/v2/pkg/util" |
| 14 | +) |
| 15 | + |
| 16 | +// goCheckpoint struct to adapt GoSdk Checkpoint |
| 17 | +type goCheckpoint struct { |
| 18 | + Checkpoint struct { |
| 19 | + SequenceNumber int64 `json:"sequenceNumber"` |
| 20 | + Offset string `json:"offset"` |
| 21 | + } `json:"checkpoint"` |
| 22 | + PartitionID string `json:"partitionId"` |
| 23 | +} |
| 24 | + |
| 25 | +// Checkpoint is the object eventhub processor stores in storage |
| 26 | +// for checkpointing event processors. This matches the object |
| 27 | +// stored by the eventhub C# sdk and Java sdk |
| 28 | +type Checkpoint struct { |
| 29 | + Epoch int64 `json:"Epoch"` |
| 30 | + Offset string `json:"Offset"` |
| 31 | + Owner string `json:"Owner"` |
| 32 | + Token string `json:"Token"` |
| 33 | + PartitionID string `json:"PartitionId"` |
| 34 | + SequenceNumber int64 `json:"SequenceNumber"` |
| 35 | +} |
| 36 | + |
| 37 | +type checkpointer interface { |
| 38 | + resolvePath(info EventHubInfo) (*url.URL, error) |
| 39 | + extractCheckpoint(get *azblob.DownloadResponse) (Checkpoint, error) |
| 40 | +} |
| 41 | + |
| 42 | +type azureWebjobCheckpointer struct { |
| 43 | + partitionID string |
| 44 | + containerName string |
| 45 | +} |
| 46 | + |
| 47 | +type defaultCheckpointer struct { |
| 48 | + partitionID string |
| 49 | + containerName string |
| 50 | +} |
| 51 | + |
| 52 | +type goSdkCheckpointer struct { |
| 53 | + partitionID string |
| 54 | + containerName string |
| 55 | +} |
| 56 | + |
| 57 | +// GetCheckpointFromBlobStorage reads depending of the CheckpointType the checkpoint from a azure storage |
| 58 | +func GetCheckpointFromBlobStorage(ctx context.Context, httpClient util.HTTPDoer, info EventHubInfo, partitionID string) (Checkpoint, error) { |
| 59 | + |
| 60 | + checkpointer := newCheckpointer(info, partitionID) |
| 61 | + return getCheckpoint(ctx, httpClient, info, checkpointer) |
| 62 | +} |
| 63 | + |
| 64 | +func newCheckpointer(info EventHubInfo, partitionID string) checkpointer { |
| 65 | + if info.CheckpointType == "GoSdk" { |
| 66 | + return &goSdkCheckpointer{ |
| 67 | + containerName: info.BlobContainer, |
| 68 | + partitionID: partitionID, |
| 69 | + } |
| 70 | + } else if info.CheckpointType == "AzureWebJob" || info.BlobContainer == "" { |
| 71 | + return &azureWebjobCheckpointer{ |
| 72 | + containerName: "azure-webjobs-eventhub", |
| 73 | + partitionID: partitionID, |
| 74 | + } |
| 75 | + } else { |
| 76 | + return &defaultCheckpointer{ |
| 77 | + containerName: info.BlobContainer, |
| 78 | + partitionID: partitionID, |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func (checkpointer *azureWebjobCheckpointer) resolvePath(info EventHubInfo) (*url.URL, error) { |
| 84 | + eventHubNamespace, eventHubName, err := getHubAndNamespace(info) |
| 85 | + if err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + |
| 89 | + // URL format - <storageEndpoint>/azure-webjobs-eventhub/<eventHubNamespace>/<eventHubName>/<eventHubConsumerGroup>/<partitionID> |
| 90 | + path, _ := url.Parse(fmt.Sprintf("/%s/%s/%s/%s/%s", checkpointer.containerName, eventHubNamespace, eventHubName, info.EventHubConsumerGroup, checkpointer.partitionID)) |
| 91 | + |
| 92 | + return path, nil |
| 93 | +} |
| 94 | + |
| 95 | +func (checkpointer *defaultCheckpointer) resolvePath(info EventHubInfo) (*url.URL, error) { |
| 96 | + eventHubNamespace, eventHubName, err := getHubAndNamespace(info) |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + |
| 101 | + // URL format - <storageEndpoint>/azure-webjobs-eventhub/<eventHubNamespace>/<eventHubName>/<eventHubConsumerGroup>/<partitionID> |
| 102 | + path, _ := url.Parse(fmt.Sprintf("/%s/%s/%s/%s/checkpoint/%s", checkpointer.containerName, eventHubNamespace, eventHubName, info.EventHubConsumerGroup, checkpointer.partitionID)) |
| 103 | + |
| 104 | + return path, nil |
| 105 | +} |
| 106 | + |
| 107 | +// Resolve Path for AzureWebJob Checkpoint |
| 108 | +func (checkpointer *goSdkCheckpointer) resolvePath(info EventHubInfo) (*url.URL, error) { |
| 109 | + path, _ := url.Parse(fmt.Sprintf("/%s/%s", info.BlobContainer, checkpointer.partitionID)) |
| 110 | + |
| 111 | + return path, nil |
| 112 | +} |
| 113 | + |
| 114 | +func (checkpointer *azureWebjobCheckpointer) extractCheckpoint(get *azblob.DownloadResponse) (Checkpoint, error) { |
| 115 | + var checkpoint Checkpoint |
| 116 | + err := readToCheckpointFromBody(get, &checkpoint) |
| 117 | + if err != nil { |
| 118 | + return Checkpoint{}, err |
| 119 | + } |
| 120 | + |
| 121 | + return checkpoint, nil |
| 122 | +} |
| 123 | + |
| 124 | +func (checkpointer *defaultCheckpointer) extractCheckpoint(get *azblob.DownloadResponse) (Checkpoint, error) { |
| 125 | + return getCheckpointFromStorageMetadata(get, checkpointer.partitionID) |
| 126 | +} |
| 127 | + |
| 128 | +func (checkpointer *goSdkCheckpointer) extractCheckpoint(get *azblob.DownloadResponse) (Checkpoint, error) { |
| 129 | + var checkpoint goCheckpoint |
| 130 | + err := readToCheckpointFromBody(get, &checkpoint) |
| 131 | + if err != nil { |
| 132 | + return Checkpoint{}, err |
| 133 | + } |
| 134 | + |
| 135 | + return Checkpoint{ |
| 136 | + SequenceNumber: checkpoint.Checkpoint.SequenceNumber, |
| 137 | + Offset: checkpoint.Checkpoint.Offset, |
| 138 | + PartitionID: checkpoint.PartitionID, |
| 139 | + }, nil |
| 140 | +} |
| 141 | + |
| 142 | +func getCheckpoint(ctx context.Context, httpClient util.HTTPDoer, info EventHubInfo, checkpointer checkpointer) (Checkpoint, error) { |
| 143 | + blobCreds, storageEndpoint, err := ParseAzureStorageBlobConnection(httpClient, kedav1alpha1.PodIdentityProviderNone, info.StorageConnection, "") |
| 144 | + if err != nil { |
| 145 | + return Checkpoint{}, err |
| 146 | + } |
| 147 | + |
| 148 | + path, err := checkpointer.resolvePath(info) |
| 149 | + if err != nil { |
| 150 | + return Checkpoint{}, err |
| 151 | + } |
| 152 | + |
| 153 | + baseURL := storageEndpoint.ResolveReference(path) |
| 154 | + |
| 155 | + get, err := downloadBlob(ctx, baseURL, blobCreds) |
| 156 | + if err != nil { |
| 157 | + return Checkpoint{}, err |
| 158 | + } |
| 159 | + |
| 160 | + return checkpointer.extractCheckpoint(get) |
| 161 | +} |
| 162 | + |
| 163 | +func getCheckpointFromStorageMetadata(get *azblob.DownloadResponse, partitionID string) (Checkpoint, error) { |
| 164 | + checkpoint := Checkpoint{ |
| 165 | + PartitionID: partitionID, |
| 166 | + } |
| 167 | + |
| 168 | + metadata := get.NewMetadata() |
| 169 | + |
| 170 | + if sequencenumber, ok := metadata["sequencenumber"]; ok { |
| 171 | + if !ok { |
| 172 | + if sequencenumber, ok = metadata["Sequencenumber"]; !ok { |
| 173 | + return Checkpoint{}, fmt.Errorf("sequencenumber on blob not found") |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + if sn, err := strconv.ParseInt(sequencenumber, 10, 64); err == nil { |
| 178 | + checkpoint.SequenceNumber = sn |
| 179 | + } else { |
| 180 | + return Checkpoint{}, fmt.Errorf("sequencenumber is not a valid int64 value: %w", err) |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + if offset, ok := metadata["offset"]; ok { |
| 185 | + if !ok { |
| 186 | + if offset, ok = metadata["Offset"]; !ok { |
| 187 | + return Checkpoint{}, fmt.Errorf("offset on blob not found") |
| 188 | + } |
| 189 | + } |
| 190 | + checkpoint.Offset = offset |
| 191 | + } |
| 192 | + |
| 193 | + return checkpoint, nil |
| 194 | +} |
| 195 | + |
| 196 | +func readToCheckpointFromBody(get *azblob.DownloadResponse, checkpoint interface{}) error { |
| 197 | + blobData := &bytes.Buffer{} |
| 198 | + |
| 199 | + reader := get.Body(azblob.RetryReaderOptions{}) |
| 200 | + if _, err := blobData.ReadFrom(reader); err != nil { |
| 201 | + return fmt.Errorf("failed to read blob data: %s", err) |
| 202 | + } |
| 203 | + defer reader.Close() // The client must close the response body when finished with it |
| 204 | + |
| 205 | + if err := json.Unmarshal(blobData.Bytes(), &checkpoint); err != nil { |
| 206 | + return fmt.Errorf("failed to decode blob data: %s", err) |
| 207 | + } |
| 208 | + |
| 209 | + return nil |
| 210 | +} |
| 211 | + |
| 212 | +func downloadBlob(ctx context.Context, baseURL *url.URL, blobCreds azblob.Credential) (*azblob.DownloadResponse, error) { |
| 213 | + blobURL := azblob.NewBlockBlobURL(*baseURL, azblob.NewPipeline(blobCreds, azblob.PipelineOptions{})) |
| 214 | + |
| 215 | + get, err := blobURL.Download(ctx, 0, 0, azblob.BlobAccessConditions{}, false, azblob.ClientProvidedKeyOptions{}) |
| 216 | + if err != nil { |
| 217 | + return nil, fmt.Errorf("unable to download file from blob storage: %w", err) |
| 218 | + } |
| 219 | + return get, nil |
| 220 | +} |
0 commit comments