Skip to content

Commit 0be515c

Browse files
committed
Use temp directory instead of resources directory
1 parent 8c1c97e commit 0be515c

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

athena-msk/src/main/java/com/amazonaws/athena/connectors/msk/GlueRegistryReader.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020
package com.amazonaws.athena.connectors.msk;
2121

22+
import com.amazonaws.athena.connector.lambda.exceptions.AthenaConnectorException;
2223
import com.amazonaws.athena.connectors.msk.dto.MSKField;
2324
import com.fasterxml.jackson.databind.DeserializationFeature;
2425
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -27,6 +28,8 @@
2728
import com.google.protobuf.DescriptorProtos.FieldDescriptorProto;
2829
import com.google.protobuf.DescriptorProtos.FileDescriptorSet;
2930
import software.amazon.awssdk.services.glue.GlueClient;
31+
import software.amazon.awssdk.services.glue.model.ErrorDetails;
32+
import software.amazon.awssdk.services.glue.model.FederationSourceErrorCode;
3033
import software.amazon.awssdk.services.glue.model.GetSchemaRequest;
3134
import software.amazon.awssdk.services.glue.model.GetSchemaResponse;
3235
import software.amazon.awssdk.services.glue.model.GetSchemaVersionRequest;
@@ -35,16 +38,17 @@
3538
import software.amazon.awssdk.services.glue.model.SchemaVersionNumber;
3639

3740
import java.io.FileInputStream;
41+
import java.io.IOException;
3842
import java.nio.file.Files;
3943
import java.nio.file.Path;
4044
import java.nio.file.Paths;
4145
import java.util.ArrayList;
4246
import java.util.List;
47+
import java.util.UUID;
4348

4449
public class GlueRegistryReader
4550
{
4651
private static final ObjectMapper objectMapper;
47-
private static final String PROTO_DIR = "src/main/resources/proto";
4852
private static final String PROTO_FILE = "schema.proto";
4953
private static final String DESC_FILE = "schema.desc";
5054

@@ -67,7 +71,8 @@ public List<MSKField> getProtobufFields(String glueRegistryName, String glueSche
6771
GetSchemaVersionResponse schemaVersionResponse = getSchemaVersionResult(glueRegistryName, glueSchemaName);
6872
String schemaDef = schemaVersionResponse.schemaDefinition();
6973

70-
Path protoDir = Paths.get(PROTO_DIR);
74+
// Create a unique temp directory using UUID
75+
Path protoDir = Paths.get("/tmp", "proto_" + UUID.randomUUID());
7176
Files.createDirectories(protoDir);
7277
Path protoFile = protoDir.resolve(PROTO_FILE);
7378
Path descFile = protoDir.resolve(DESC_FILE);
@@ -107,8 +112,20 @@ public List<MSKField> getProtobufFields(String glueRegistryName, String glueSche
107112
return fields;
108113
}
109114
finally {
110-
Files.deleteIfExists(protoFile);
111-
Files.deleteIfExists(descFile);
115+
// Clean up temporary files
116+
try {
117+
Files.deleteIfExists(protoFile);
118+
Files.deleteIfExists(descFile);
119+
Files.deleteIfExists(protoDir);
120+
}
121+
catch (IOException e) {
122+
throw new AthenaConnectorException(
123+
"Failed to clean up temporary proto directory: " + protoDir.toAbsolutePath(),
124+
ErrorDetails.builder()
125+
.errorCode(FederationSourceErrorCode.INTERNAL_SERVICE_EXCEPTION.toString())
126+
.build()
127+
);
128+
}
112129
}
113130
}
114131
/**

athena-msk/src/test/java/com/amazonaws/athena/connectors/msk/AmazonMskRecordHandlerTest.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.amazonaws.athena.connector.lambda.domain.predicate.ConstraintEvaluator;
3131
import com.amazonaws.athena.connector.lambda.domain.predicate.Constraints;
3232
import com.amazonaws.athena.connector.lambda.domain.spill.S3SpillLocation;
33+
import com.amazonaws.athena.connector.lambda.exceptions.AthenaConnectorException;
3334
import com.amazonaws.athena.connector.lambda.records.ReadRecordsRequest;
3435
import com.amazonaws.athena.connector.lambda.security.EncryptionKey;
3536
import com.amazonaws.athena.connector.lambda.security.EncryptionKeyFactory;
@@ -70,6 +71,8 @@
7071
import org.mockito.junit.MockitoJUnitRunner;
7172
import software.amazon.awssdk.services.athena.AthenaClient;
7273
import software.amazon.awssdk.services.glue.GlueClient;
74+
import software.amazon.awssdk.services.glue.model.ErrorDetails;
75+
import software.amazon.awssdk.services.glue.model.FederationSourceErrorCode;
7376
import software.amazon.awssdk.services.glue.model.GetSchemaRequest;
7477
import software.amazon.awssdk.services.glue.model.GetSchemaResponse;
7578
import software.amazon.awssdk.services.glue.model.GetSchemaVersionRequest;
@@ -78,6 +81,7 @@
7881
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient;
7982

8083
import java.io.FileInputStream;
84+
import java.io.IOException;
8185
import java.nio.file.Files;
8286
import java.nio.file.Path;
8387
import java.nio.file.Paths;
@@ -515,9 +519,9 @@ private Schema createAvroSchema(AvroTopicSchema avroTopicSchema) throws Exceptio
515519
private Schema createProtobufSchema(String protobufSchema) throws Exception
516520
{
517521
SchemaBuilder schemaBuilder = SchemaBuilder.newBuilder();
518-
519-
// Create a temporary directory and files
520-
Path protoDir = Paths.get("src/test/resources/proto");
522+
523+
// Use a unique temporary directory under /tmp
524+
Path protoDir = Paths.get("/tmp", "proto_" + UUID.randomUUID());
521525
Files.createDirectories(protoDir);
522526
Path protoFile = protoDir.resolve("test.proto");
523527
Path descFile = protoDir.resolve("test.desc");
@@ -557,15 +561,26 @@ private Schema createProtobufSchema(String protobufSchema) throws Exception
557561
Field arrowField = new Field(field.getName(), arrowFieldType, null);
558562
schemaBuilder.addField(arrowField);
559563
}
560-
} else {
564+
}
565+
else {
561566
throw new RuntimeException("No message types found in compiled schema");
562567
}
563568
}
564-
} finally {
565-
// Clean up
566-
Files.deleteIfExists(protoFile);
567-
Files.deleteIfExists(descFile);
568-
Files.deleteIfExists(protoDir);
569+
}
570+
finally {
571+
try {
572+
Files.deleteIfExists(protoFile);
573+
Files.deleteIfExists(descFile);
574+
Files.deleteIfExists(protoDir);
575+
}
576+
catch (IOException e) {
577+
throw new AthenaConnectorException(
578+
"Failed to clean up temporary proto directory: " + protoDir.toAbsolutePath(),
579+
ErrorDetails.builder()
580+
.errorCode(FederationSourceErrorCode.INTERNAL_SERVICE_EXCEPTION.toString())
581+
.build()
582+
);
583+
}
569584
}
570585

571586
schemaBuilder.addMetadata("dataFormat", PROTOBUF_DATA_FORMAT);

0 commit comments

Comments
 (0)