Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor file syncer #3539

Merged
merged 11 commits into from
Nov 15, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import lombok.experimental.Accessors;
import org.prebid.server.auction.model.AuctionContext;
import org.prebid.server.auction.model.BidRejectionTracker;
import org.prebid.server.execution.Timeout;
import org.prebid.server.execution.timeout.Timeout;
import org.prebid.server.hooks.v1.bidder.BidderInvocationContext;
import org.prebid.server.model.Endpoint;
import org.prebid.server.proto.openrtb.ext.request.ExtRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
import org.prebid.server.events.EventsService;
import org.prebid.server.exception.InvalidRequestException;
import org.prebid.server.exception.PreBidException;
import org.prebid.server.execution.Timeout;
import org.prebid.server.execution.timeout.Timeout;
import org.prebid.server.hooks.execution.HookStageExecutor;
import org.prebid.server.hooks.execution.model.HookStageExecutionResult;
import org.prebid.server.hooks.v1.bidder.AllProcessedBidResponsesPayload;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/prebid/server/auction/ExchangeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
import org.prebid.server.cookie.UidsCookie;
import org.prebid.server.exception.InvalidRequestException;
import org.prebid.server.exception.PreBidException;
import org.prebid.server.execution.Timeout;
import org.prebid.server.execution.TimeoutFactory;
import org.prebid.server.execution.timeout.Timeout;
import org.prebid.server.execution.timeout.TimeoutFactory;
import org.prebid.server.floors.PriceFloorAdjuster;
import org.prebid.server.floors.PriceFloorProcessor;
import org.prebid.server.hooks.execution.HookStageExecutor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.prebid.server.auction.model.AuctionContext;
import org.prebid.server.auction.model.IpAddress;
import org.prebid.server.auction.requestfactory.Ortb2ImplicitParametersResolver;
import org.prebid.server.execution.Timeout;
import org.prebid.server.execution.timeout.Timeout;
import org.prebid.server.geolocation.GeoLocationService;
import org.prebid.server.geolocation.model.GeoInfo;
import org.prebid.server.log.Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.prebid.server.auction.model.AuctionContext;
import org.prebid.server.auction.model.StoredResponseResult;
import org.prebid.server.exception.InvalidRequestException;
import org.prebid.server.execution.Timeout;
import org.prebid.server.execution.timeout.Timeout;
import org.prebid.server.proto.openrtb.ext.request.ExtRequest;
import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebid;
import org.prebid.server.proto.openrtb.ext.request.ExtStoredAuctionResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import org.prebid.server.exception.InvalidRequestException;
import org.prebid.server.exception.InvalidStoredImpException;
import org.prebid.server.exception.InvalidStoredRequestException;
import org.prebid.server.execution.Timeout;
import org.prebid.server.execution.TimeoutFactory;
import org.prebid.server.execution.timeout.Timeout;
import org.prebid.server.execution.timeout.TimeoutFactory;
import org.prebid.server.identity.IdGenerator;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.json.JsonMerger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.prebid.server.bidder.model.BidderSeatBid;
import org.prebid.server.exception.InvalidRequestException;
import org.prebid.server.exception.PreBidException;
import org.prebid.server.execution.Timeout;
import org.prebid.server.execution.timeout.Timeout;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.proto.openrtb.ext.request.ExtImp;
import org.prebid.server.proto.openrtb.ext.request.ExtImpPrebid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.prebid.server.auction.model.Tuple2;
import org.prebid.server.auction.model.WithPodErrors;
import org.prebid.server.exception.InvalidRequestException;
import org.prebid.server.execution.TimeoutFactory;
import org.prebid.server.execution.timeout.TimeoutFactory;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.json.JsonMerger;
import org.prebid.server.log.Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.prebid.server.bidder.model.BidderSeatBid;
import org.prebid.server.exception.InvalidRequestException;
import org.prebid.server.exception.PreBidException;
import org.prebid.server.execution.Timeout;
import org.prebid.server.execution.timeout.Timeout;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.proto.openrtb.ext.ExtIncludeBrandCategory;
import org.prebid.server.proto.openrtb.ext.request.ExtDealTier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io.vertx.core.Future;
import org.prebid.server.auction.model.BidderResponse;
import org.prebid.server.auction.model.CategoryMappingResult;
import org.prebid.server.execution.Timeout;
import org.prebid.server.execution.timeout.Timeout;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io.vertx.core.Future;
import org.prebid.server.auction.model.BidderResponse;
import org.prebid.server.auction.model.CategoryMappingResult;
import org.prebid.server.execution.Timeout;
import org.prebid.server.execution.timeout.Timeout;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.prebid.server.auction.gpp.model.GppContext;
import org.prebid.server.bidder.UsersyncMethodType;
import org.prebid.server.cookie.UidsCookie;
import org.prebid.server.execution.Timeout;
import org.prebid.server.execution.timeout.Timeout;
import org.prebid.server.privacy.model.PrivacyContext;
import org.prebid.server.settings.model.Account;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.prebid.server.auction.model;

import lombok.Value;
import org.prebid.server.execution.Timeout;
import org.prebid.server.execution.timeout.Timeout;

@Value(staticConstructor = "of")
public class TimeoutContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.prebid.server.auction.ImplicitParametersExtractor;
import org.prebid.server.auction.IpAddressHelper;
import org.prebid.server.auction.model.IpAddress;
import org.prebid.server.execution.Timeout;
import org.prebid.server.execution.timeout.Timeout;
import org.prebid.server.metric.MetricName;
import org.prebid.server.privacy.PrivacyExtractor;
import org.prebid.server.privacy.gdpr.TcfDefinerService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.prebid.server.auction.ImplicitParametersExtractor;
import org.prebid.server.auction.IpAddressHelper;
import org.prebid.server.auction.model.IpAddress;
import org.prebid.server.execution.Timeout;
import org.prebid.server.execution.timeout.Timeout;
import org.prebid.server.metric.MetricName;
import org.prebid.server.privacy.PrivacyExtractor;
import org.prebid.server.privacy.gdpr.TcfDefinerService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
import org.prebid.server.exception.InvalidRequestException;
import org.prebid.server.exception.PreBidException;
import org.prebid.server.exception.UnauthorizedAccountException;
import org.prebid.server.execution.Timeout;
import org.prebid.server.execution.TimeoutFactory;
import org.prebid.server.execution.timeout.Timeout;
import org.prebid.server.execution.timeout.TimeoutFactory;
import org.prebid.server.geolocation.CountryCodeMapper;
import org.prebid.server.geolocation.model.GeoInfo;
import org.prebid.server.hooks.execution.HookStageExecutor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.prebid.server.bidder.model.HttpResponse;
import org.prebid.server.bidder.model.Result;
import org.prebid.server.exception.PreBidException;
import org.prebid.server.execution.Timeout;
import org.prebid.server.execution.timeout.Timeout;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.log.Logger;
import org.prebid.server.log.LoggerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.prebid.server.events.EventsContext;
import org.prebid.server.events.EventsService;
import org.prebid.server.exception.PreBidException;
import org.prebid.server.execution.Timeout;
import org.prebid.server.execution.timeout.Timeout;
import org.prebid.server.identity.UUIDIdGenerator;
import org.prebid.server.json.DecodeException;
import org.prebid.server.json.JacksonMapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.prebid.server.auction.gpp.model.GppContext;
import org.prebid.server.bidder.UsersyncMethodChooser;
import org.prebid.server.cookie.UidsCookie;
import org.prebid.server.execution.Timeout;
import org.prebid.server.execution.timeout.Timeout;
import org.prebid.server.privacy.model.PrivacyContext;
import org.prebid.server.proto.request.CookieSyncRequest;
import org.prebid.server.settings.model.Account;
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/org/prebid/server/execution/RemoteFileProcessor.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.prebid.server.execution.file;

import io.vertx.core.Future;

public interface FileProcessor {

Future<?> setDataPath(String dataFilePath);
}
106 changes: 106 additions & 0 deletions src/main/java/org/prebid/server/execution/file/FileUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package org.prebid.server.execution.file;

import io.vertx.core.Vertx;
import io.vertx.core.file.FileProps;
import io.vertx.core.file.FileSystem;
import io.vertx.core.file.FileSystemException;
import io.vertx.core.http.HttpClientOptions;
import org.apache.commons.lang3.ObjectUtils;
import org.prebid.server.exception.PreBidException;
import org.prebid.server.execution.file.syncer.FileSyncer;
import org.prebid.server.execution.file.syncer.LocalFileSyncer;
import org.prebid.server.execution.file.syncer.RemoteFileSyncerV2;
import org.prebid.server.execution.retry.ExponentialBackoffRetryPolicy;
import org.prebid.server.execution.retry.FixedIntervalRetryPolicy;
import org.prebid.server.execution.retry.RetryPolicy;
import org.prebid.server.spring.config.model.ExponentialBackoffProperties;
import org.prebid.server.spring.config.model.FileSyncerProperties;
import org.prebid.server.spring.config.model.HttpClientProperties;

import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileUtil {

private FileUtil() {
}

public static void createAndCheckWritePermissionsFor(FileSystem fileSystem, String filePath) {
try {
final Path dirPath = Paths.get(filePath).getParent();
final String dirPathString = dirPath.toString();
final FileProps props = fileSystem.existsBlocking(dirPathString)
? fileSystem.propsBlocking(dirPathString)
: null;

if (props == null || !props.isDirectory()) {
fileSystem.mkdirsBlocking(dirPathString);
} else if (!Files.isWritable(dirPath)) {
throw new PreBidException("No write permissions for directory: " + dirPath);
}
} catch (FileSystemException | InvalidPathException e) {
throw new PreBidException("Cannot create directory for file: " + filePath, e);
}
}

public static FileSyncer fileSyncerFor(FileProcessor fileProcessor,
FileSyncerProperties properties,
Vertx vertx) {

return switch (properties.getType()) {
case LOCAL -> new LocalFileSyncer(
fileProcessor,
properties.getSaveFilepath(),
properties.getUpdateIntervalMs(),
toRetryPolicy(properties),
vertx);
case REMOTE -> remoteFileSyncer(fileProcessor, properties, vertx);
};
}

private static RemoteFileSyncerV2 remoteFileSyncer(FileProcessor fileProcessor,
FileSyncerProperties properties,
Vertx vertx) {

final HttpClientProperties httpClientProperties = properties.getHttpClient();
final HttpClientOptions httpClientOptions = new HttpClientOptions()
.setConnectTimeout(httpClientProperties.getConnectTimeoutMs())
.setMaxRedirects(httpClientProperties.getMaxRedirects());

return new RemoteFileSyncerV2(
fileProcessor,
properties.getDownloadUrl(),
properties.getSaveFilepath(),
properties.getTmpFilepath(),
vertx.createHttpClient(httpClientOptions),
properties.getTimeoutMs(),
properties.isCheckSize(),
properties.getUpdateIntervalMs(),
toRetryPolicy(properties),
vertx);
}

// TODO: remove after transition period
private static RetryPolicy toRetryPolicy(FileSyncerProperties properties) {
final Long retryIntervalMs = properties.getRetryIntervalMs();
final Integer retryCount = properties.getRetryCount();
final boolean fixedRetryPolicyDefined = ObjectUtils.anyNotNull(retryIntervalMs, retryCount);
final boolean fixedRetryPolicyValid = ObjectUtils.allNotNull(retryIntervalMs, retryCount)
|| !fixedRetryPolicyDefined;

if (!fixedRetryPolicyValid) {
throw new IllegalArgumentException("fixed interval retry policy is invalid");
}

final ExponentialBackoffProperties exponentialBackoffProperties = properties.getRetry();
return fixedRetryPolicyDefined
? FixedIntervalRetryPolicy.limited(retryIntervalMs, retryCount)
: ExponentialBackoffRetryPolicy.of(
exponentialBackoffProperties.getDelayMillis(),
exponentialBackoffProperties.getMaxDelayMillis(),
exponentialBackoffProperties.getFactor(),
exponentialBackoffProperties.getJitter());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.prebid.server.execution.file.supplier;

import io.vertx.core.Future;
import io.vertx.core.file.FileProps;
import io.vertx.core.file.FileSystem;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;

public class LocalFileSupplier implements Supplier<Future<String>> {

private final String filePath;
private final FileSystem fileSystem;
private final AtomicLong lastSupplyTime;

public LocalFileSupplier(String filePath, FileSystem fileSystem) {
this.filePath = Objects.requireNonNull(filePath);
this.fileSystem = Objects.requireNonNull(fileSystem);
lastSupplyTime = new AtomicLong(Long.MIN_VALUE);
}

@Override
public Future<String> get() {
return fileSystem.exists(filePath)
.compose(exists -> exists
? fileSystem.props(filePath)
: Future.failedFuture("File %s not found.".formatted(filePath)))
.map(this::getFileIfModified);
}

private String getFileIfModified(FileProps fileProps) {
final long lastModifiedTime = lasModifiedTime(fileProps);
final long lastSupplyTime = this.lastSupplyTime.get();

if (lastSupplyTime < lastModifiedTime) {
this.lastSupplyTime.compareAndSet(lastSupplyTime, lastModifiedTime);
return filePath;
}

return null;
}

private static long lasModifiedTime(FileProps fileProps) {
return Math.max(fileProps.creationTime(), fileProps.lastModifiedTime());
}
}
Loading
Loading