6
6
7
7
package com .sap .cx .boosters .commercedbsync .logging ;
8
8
9
- import com .microsoft .azure .storage .CloudStorageAccount ;
10
- import com .microsoft .azure .storage .blob .CloudAppendBlob ;
11
- import com .microsoft .azure .storage .blob .CloudBlobClient ;
12
- import com .microsoft .azure .storage .blob .CloudBlobContainer ;
13
- import com .microsoft .azure .storage .blob .CloudBlobDirectory ;
14
- import com .microsoft .azure .storage .blob .CloudBlockBlob ;
9
+ import com .azure .storage .blob .BlobContainerClient ;
10
+ import com .azure .storage .blob .BlobServiceClient ;
11
+ import com .azure .storage .blob .BlobServiceClientBuilder ;
12
+ import com .azure .storage .blob .specialized .AppendBlobClient ;
13
+ import com .azure .storage .blob .specialized .BlockBlobClient ;
15
14
16
15
import java .io .ByteArrayInputStream ;
17
16
import java .io .ByteArrayOutputStream ;
18
17
import java .io .InputStream ;
19
- import java .net .URISyntaxException ;
20
18
import java .nio .charset .StandardCharsets ;
21
- import java .security .InvalidKeyException ;
22
19
import java .util .ArrayList ;
23
20
import java .util .Collection ;
24
21
import java .util .Collections ;
@@ -45,6 +42,7 @@ public class JDBCQueriesStore {
45
42
private final String dbConnectionString ;
46
43
private final Collection <JdbcQueryLog > queryLogs ;
47
44
private final MigrationContext context ;
45
+ private BlobServiceClient blobServiceClient ;
48
46
49
47
private final boolean isSourceDB ;
50
48
// Unique id of the file in file storage where the jdbc store(s) across
@@ -105,16 +103,14 @@ public void writeToLogFileAndCompress(final String migrationId) {
105
103
}
106
104
107
105
public Pair <byte [], String > getLogFile (final String migrationId ) {
108
- final String logFileName = getLogFileName (migrationId , true );
109
106
try (ByteArrayOutputStream baos = new ByteArrayOutputStream ()) {
110
- CloudBlobDirectory jdbcLogsDirectory = getContainer ().getDirectoryReference ("jdbclogs" );
111
- CloudBlockBlob zippedLogBlobFile = jdbcLogsDirectory .getBlockBlobReference (logFileName );
107
+ final BlockBlobClient zippedLogBlobFile = getZippedLogBlobFile (migrationId );
112
108
zippedLogBlobFile .download (baos );
113
- return Pair .of (baos .toByteArray (), logFileName );
109
+ return Pair .of (baos .toByteArray (), getLogFileName ( migrationId , true ) );
114
110
} catch (Exception e ) {
115
111
String errorMessage = String .format (
116
112
"Log file %s for datasource %s does not exist in storage %s or is currently being created" ,
117
- logFileName , dbConnectionString , context .getFileStorageContainerName ());
113
+ getLogFileName ( migrationId , true ) , dbConnectionString , context .getFileStorageContainerName ());
118
114
LOG .error (errorMessage , e );
119
115
return Pair .of (errorMessage .getBytes (StandardCharsets .UTF_8 ), getLogFileName (migrationId , false ));
120
116
}
@@ -127,8 +123,7 @@ public String toString() {
127
123
128
124
private void flushQueryLogsToAppendingFile () {
129
125
try {
130
- CloudBlobDirectory jdbcLogsDirectory = getContainer ().getDirectoryReference (JDBCLOGS_DIRECTORY );
131
- CloudAppendBlob sharedStoreLogFile = jdbcLogsDirectory .getAppendBlobReference (sharedStoreLogFileName );
126
+ final AppendBlobClient sharedStoreLogFile = getSharedStoreLogFile ();
132
127
byte [] queryLogsBytes = getQueryLogsAsString ().getBytes (StandardCharsets .UTF_8 .name ());
133
128
try (InputStream is = new ByteArrayInputStream (queryLogsBytes )) {
134
129
sharedStoreLogFile .appendBlock (is , queryLogsBytes .length );
@@ -148,13 +143,11 @@ private String getQueryLogsAsString() {
148
143
149
144
private void compressAppendingFileContent (final String migrationId ) {
150
145
try (ByteArrayOutputStream baos = new ByteArrayOutputStream ()) {
151
- CloudBlobDirectory jdbcLogsDirectory = getContainer ().getDirectoryReference (JDBCLOGS_DIRECTORY );
152
- CloudAppendBlob sharedStoreLogFile = jdbcLogsDirectory .getAppendBlobReference (this .sharedStoreLogFileName );
153
- sharedStoreLogFile .download (baos );
146
+ final AppendBlobClient sharedStoreLogFile = getSharedStoreLogFile ();
147
+ sharedStoreLogFile .downloadStream (baos );
154
148
byte [] zippedLogBytes = FileUtils .zipBytes (getLogFileName (migrationId , false ), baos .toByteArray ());
155
- CloudBlockBlob zippedLogBlobFile = jdbcLogsDirectory
156
- .getBlockBlobReference (getLogFileName (migrationId , true ));
157
- zippedLogBlobFile .uploadFromByteArray (zippedLogBytes , 0 , zippedLogBytes .length );
149
+ final BlockBlobClient zippedLogBlobFile = getZippedLogBlobFile (migrationId );
150
+ zippedLogBlobFile .upload (new ByteArrayInputStream (zippedLogBytes ), zippedLogBytes .length );
158
151
} catch (Exception e ) {
159
152
LOG .error ("Failed to compress query logs from file {} in storage {} for datasource {}" ,
160
153
getLogFileName (migrationId , false ), context .getFileStorageConnectionString (), dbConnectionString ,
@@ -164,34 +157,43 @@ private void compressAppendingFileContent(final String migrationId) {
164
157
165
158
private void resetAppendingFile () {
166
159
try {
167
- CloudBlobClient blobClient = getCloudBlobClient ();
168
- CloudBlobDirectory jdbcLogsDirectory = blobClient
169
- .getContainerReference (context .getFileStorageContainerName ()).getDirectoryReference ("jdbclogs" );
170
- CloudAppendBlob logBlobFile = jdbcLogsDirectory .getAppendBlobReference (sharedStoreLogFileName );
171
- logBlobFile .createOrReplace ();
160
+ getSharedStoreLogFile ().create (true );
172
161
} catch (Exception e ) {
173
162
LOG .error ("Failed to create or replace appending file {} in storage {} for datasource {}" ,
174
163
sharedStoreLogFileName , context .getFileStorageContainerName (), dbConnectionString , e );
175
164
}
176
165
}
177
166
178
- private CloudBlobClient getCloudBlobClient () throws URISyntaxException , InvalidKeyException {
179
- // if file storage connection string is not set, do not try to connect to the
180
- // storage
167
+ protected BlobServiceClient getBlobServiceClient () throws Exception {
181
168
if (context .getFileStorageConnectionString () == null ) {
182
169
throw new IllegalArgumentException ("File storage connection string not set" );
183
170
}
184
- CloudStorageAccount account = CloudStorageAccount .parse (context .getFileStorageConnectionString ());
185
- return account .createCloudBlobClient ();
171
+
172
+ if (blobServiceClient == null ) {
173
+ blobServiceClient = new BlobServiceClientBuilder ()
174
+ .connectionString (context .getFileStorageConnectionString ()).buildClient ();
175
+ }
176
+
177
+ return blobServiceClient ;
186
178
}
187
179
188
- private CloudBlobContainer getContainer () throws Exception {
189
- CloudBlobContainer containerReference = getCloudBlobClient ()
190
- .getContainerReference (context .getFileStorageContainerName ());
180
+ protected BlobContainerClient getContainerClient () throws Exception {
181
+ final BlobContainerClient containerClient = getBlobServiceClient ()
182
+ .getBlobContainerClient (context .getFileStorageContainerName ());;
183
+ containerClient .createIfNotExists ();
184
+ return containerClient ;
185
+ }
191
186
192
- containerReference .createIfNotExists ();
187
+ protected AppendBlobClient getSharedStoreLogFile () throws Exception {
188
+ final AppendBlobClient appendBlobClient = getContainerClient ()
189
+ .getBlobClient (JDBCLOGS_DIRECTORY + "/" + sharedStoreLogFileName ).getAppendBlobClient ();
190
+ appendBlobClient .createIfNotExists ();
191
+ return appendBlobClient ;
192
+ }
193
193
194
- return containerReference ;
194
+ protected BlockBlobClient getZippedLogBlobFile (final String migrationId ) throws Exception {
195
+ final String logFileName = getLogFileName (migrationId , true );
196
+ return getContainerClient ().getBlobClient (JDBCLOGS_DIRECTORY + "/" + logFileName ).getBlockBlobClient ();
195
197
}
196
198
197
199
private String getLogFileName (final String migrationId , final boolean isZipped ) {
0 commit comments