This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
AzureBlob.scala
178 lines (148 loc) · 5 KB
/
AzureBlob.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.azure.iot.iothubreact.checkpointing.backends
import java.io.IOException
import java.net.URISyntaxException
import java.util.UUID
import com.microsoft.azure.iot.iothubreact.checkpointing.ICPConfiguration
import com.microsoft.azure.iot.iothubreact.scaladsl.IoTHubPartition
import com.microsoft.azure.iot.iothubreact.{Logger, Retry}
import com.microsoft.azure.storage.blob.CloudBlockBlob
import com.microsoft.azure.storage.{AccessCondition, CloudStorageAccount, OperationContext, StorageException}
import scala.concurrent.duration._
import scala.language.{implicitConversions, postfixOps}
/** Storage logic to write checkpoints to Azure blobs
*/
private[iothubreact] class AzureBlob(implicit config: ICPConfiguration) extends CheckpointBackend with Logger {
// Set the account to point either to Azure or the emulator
val account: CloudStorageAccount = if (config.azureBlobEmulator)
CloudStorageAccount.getDevelopmentStorageAccount()
else
CloudStorageAccount.parse(config.azureBlobConnectionString)
val client = account.createCloudBlobClient()
// Set the container, ensure it's ready
val container = client.getContainerReference(checkpointNamespace)
try {
Retry(2, 5 seconds) {
container.createIfNotExists()
}
} catch {
case e: StorageException ⇒ {
log.error(e, s"Err: ${e.getMessage}; Code: ${e.getErrorCode}; Status: ${e.getHttpStatusCode}")
throw e
}
case e: IOException ⇒ {
log.error(e, e.getMessage)
throw e
}
case e: Exception ⇒ {
log.error(e, e.getMessage)
throw e
}
}
/** Read the offset of the last record processed for the given partition
*
* @param partition Partition number
*
* @return Offset of the last record (already) processed
*/
override def readOffset(partition: Int): String = {
val file = getBlockBlobReference(partition)
try {
file.downloadText()
} catch {
case e: StorageException ⇒ {
if (e.getErrorCode == "BlobNotFound") {
IoTHubPartition.OffsetCheckpointNotFound
} else {
log.error(e, s"Err: ${e.getMessage}; Code: ${e.getErrorCode}; Status: ${e.getHttpStatusCode}")
throw e
}
}
case e: IOException ⇒ {
log.error(e, e.getMessage)
throw e
}
case e: Exception ⇒ {
log.error(e, e.getMessage)
throw e
}
}
}
/** Store the offset for the given IoT hub partition
*
* @param partition IoT hub partition number
* @param offset IoT hub partition offset
*/
override def writeOffset(partition: Int, offset: String): Unit = {
val file = getBlockBlobReference(partition)
val leaseId = acquireLease(file)
writeAndRelease(file, leaseId, offset)
}
private[this] def getBlockBlobReference(partition: Int): CloudBlockBlob = {
try {
Retry(2, 2 seconds) {
container.getBlockBlobReference(filename(partition))
}
} catch {
case e: StorageException ⇒ {
log.error(e, e.getMessage)
throw e
}
case e: URISyntaxException ⇒ {
log.error(e, e.getMessage)
throw e
}
case e: Exception ⇒ {
log.error(e, e.getMessage)
throw e
}
}
}
private[this] def acquireLease(file: CloudBlockBlob): String = {
// Note: the lease ID must be a Guid otherwise the service returs 400
var leaseId = UUID.randomUUID().toString
try {
file.acquireLease(config.azureBlobLeaseDuration.toSeconds.toInt, leaseId)
} catch {
case e: StorageException ⇒ {
if (e.getErrorCode == "BlobNotFound") {
leaseId = ""
} else {
log.error(e, s"Err: ${e.getMessage}; Code: ${e.getErrorCode}; Status: ${e.getHttpStatusCode}")
throw e
}
}
case e: Exception ⇒ {
log.error(e, e.getMessage)
throw e
}
}
leaseId
}
private[this] def writeAndRelease(file: CloudBlockBlob, leaseId: String, content: String): Unit = {
// The access condition depends on the file existing
val accessCondition = if (leaseId == "")
AccessCondition.generateEmptyCondition()
else
AccessCondition.generateLeaseCondition(leaseId)
try {
file.uploadText(content, "UTF-8", accessCondition, null, new OperationContext)
// If this is a new file, there is no lease to release
if (leaseId != "") file.releaseLease(accessCondition)
} catch {
case e: StorageException ⇒ {
log.error(e, e.getMessage)
throw e
}
case e: IOException ⇒ {
log.error(e, e.getMessage)
throw e
}
case e: Exception ⇒ {
log.error(e, e.getMessage)
throw e
}
}
}
private[this] def filename(partition: Int): String = "partition-" + partition
}