From 5a11d4929d5dda0956012500c6b61163d047e81e Mon Sep 17 00:00:00 2001 From: EvgeniiMunin Date: Fri, 13 Dec 2024 10:28:59 -0800 Subject: [PATCH] fetch mmdb from GCS --- .../data/config/DatabaseReaderFactory.java | 40 +++++++++++++++---- .../GreenbidsRealTimeDataConfiguration.java | 5 ++- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/DatabaseReaderFactory.java b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/DatabaseReaderFactory.java index a40c98ebb25..f5e68c20443 100644 --- a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/DatabaseReaderFactory.java +++ b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/DatabaseReaderFactory.java @@ -1,5 +1,8 @@ package org.prebid.server.hooks.modules.greenbids.real.time.data.config; +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; import io.vertx.core.Promise; import io.vertx.core.Vertx; import com.maxmind.geoip2.DatabaseReader; @@ -12,35 +15,50 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Optional; import java.util.concurrent.atomic.AtomicReference; public class DatabaseReaderFactory implements Initializable { - private final String geoLiteCountryUrl; + private final String gcsBucketName; + + private final String geoLiteCountryPath; private final Vertx vertx; + private final Storage storage; + private final AtomicReference databaseReaderRef = new AtomicReference<>(); - public DatabaseReaderFactory(String geoLitCountryUrl, Vertx vertx) { - this.geoLiteCountryUrl = geoLitCountryUrl; + public DatabaseReaderFactory(String gcsBucketName, String geoLiteCountryPath, Vertx vertx, Storage storage) { + this.gcsBucketName = gcsBucketName; + this.geoLiteCountryPath = geoLiteCountryPath; this.vertx = vertx; + this.storage = storage; } @Override public void initialize(Promise initializePromise) { - vertx.executeBlocking(() -> { try { - final URL url = new URL(geoLiteCountryUrl); + final Blob blob = getBlob(geoLiteCountryPath); final Path databasePath = Files.createTempFile("GeoLite2-Country", ".mmdb"); - try (InputStream inputStream = url.openStream(); - FileOutputStream outputStream = new FileOutputStream(databasePath.toFile())) { - inputStream.transferTo(outputStream); + try (FileOutputStream outputStream = new FileOutputStream(databasePath.toFile())) { + outputStream.write(blob.getContent()); } databaseReaderRef.set(new DatabaseReader.Builder(databasePath.toFile()).build()); + + System.out.println( + "DatabaseReaderFactory/initialize: \n" + + " gcsBucketName: " + gcsBucketName + "\n" + + " geoLiteCountryPath: " + geoLiteCountryPath + "\n" + + " blob: " + blob + "\n" + + " databasePath: " + databasePath + "\n" + + " gcsBucketName: " + gcsBucketName + ); + } catch (IOException e) { throw new PreBidException("Failed to initialize DatabaseReader from URL", e); } @@ -49,6 +67,12 @@ public void initialize(Promise initializePromise) { .onComplete(initializePromise); } + private Blob getBlob(String blobName) { + return Optional.ofNullable(storage.get(gcsBucketName)) + .map(bucket -> bucket.get(blobName)) + .orElseThrow(() -> new PreBidException("Bucket not found: " + gcsBucketName)); + } + public DatabaseReader getDatabaseReader() { return databaseReaderRef.get(); } diff --git a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/GreenbidsRealTimeDataConfiguration.java b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/GreenbidsRealTimeDataConfiguration.java index 959352d1908..66caeef2ed1 100644 --- a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/GreenbidsRealTimeDataConfiguration.java +++ b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/GreenbidsRealTimeDataConfiguration.java @@ -31,8 +31,9 @@ public class GreenbidsRealTimeDataConfiguration { @Bean - DatabaseReaderFactory databaseReaderFactory(GreenbidsRealTimeDataProperties properties, Vertx vertx) { - return new DatabaseReaderFactory(properties.getGeoLiteCountryPath(), vertx); + DatabaseReaderFactory databaseReaderFactory( + GreenbidsRealTimeDataProperties properties, Vertx vertx, Storage storage) { + return new DatabaseReaderFactory(properties.gcsBucketName, properties.getGeoLiteCountryPath(), vertx, storage); } @Bean