Skip to content

Commit

Permalink
Added background maintenance job
Browse files Browse the repository at this point in the history
Added background maintenance job
  • Loading branch information
hirenkp2000 authored Aug 4, 2023
2 parents b589559 + d9a3232 commit 4a07754
Show file tree
Hide file tree
Showing 9 changed files with 506 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.here.naksha.app.service.http.NakshaHttpVerticle;
import com.here.naksha.app.service.http.auth.NakshaAuthProvider;
import com.here.naksha.app.service.jobs.StorageMaintainer;
import com.here.naksha.lib.core.AbstractTask;
import com.here.naksha.lib.core.INaksha;
import com.here.naksha.lib.core.NakshaAdminCollection;
Expand Down Expand Up @@ -187,20 +188,25 @@ public NakshaHub(@NotNull PsqlConfig adminDbConfig, @NotNull String configId, @N
if (!existing.containsKey(NakshaAdminCollection.STORAGES.getId())) {
tx.createCollection(NakshaAdminCollection.STORAGES);
tx.commit();

}
// read default storage
Storage defStorage = tx.readFeatures(Storage.class, NakshaAdminCollection.STORAGES)
.getFeatureById("psql");
if (defStorage == null) {
try (final Json json = Json.get()) {
final String storageJson = IoHelp.readResource("config/storage.json");
final Storage storage = json.reader(ViewDeserialize.Storage.class)
defStorage = json.reader(ViewDeserialize.Storage.class)
.forType(Storage.class)
.readValue(storageJson);
// TODO HP_QUERY : How do I generate "number" for Storage class here?
tx.writeFeatures(Storage.class, NakshaAdminCollection.STORAGES)
.modifyFeatures(new ModifyFeaturesReq<Storage>(false).insert(storage));
.modifyFeatures(new ModifyFeaturesReq<Storage>(false).insert(defStorage));
tx.commit();
} catch (Exception e) {
throw unchecked(e);
}
}
this.storage = defStorage;

if (!existing.containsKey(NakshaAdminCollection.SPACES.getId())) {
tx.createCollection(NakshaAdminCollection.SPACES);
Expand All @@ -214,28 +220,32 @@ public NakshaHub(@NotNull PsqlConfig adminDbConfig, @NotNull String configId, @N
if (!existing.containsKey(NakshaAdminCollection.CONFIGS.getId())) {
tx.createCollection(NakshaAdminCollection.CONFIGS);
tx.commit();

try (final Json json = Json.get()) {
final String configJson = IoHelp.readResource("config/local.json");
config = json.reader(ViewDeserialize.Storage.class)
.forType(NakshaHubConfig.class)
.readValue(configJson);
tx.writeFeatures(NakshaHubConfig.class, NakshaAdminCollection.CONFIGS)
.modifyFeatures(new ModifyFeaturesReq<NakshaHubConfig>(false).insert(config));
tx.commit();
} catch (Exception e) {
throw unchecked(e);
}
} else {
config = tx.readFeatures(NakshaHubConfig.class, NakshaAdminCollection.CONFIGS)
.getFeatureById(configId);
// TODO HP_QUERY : Why this way instead of direct call `log.info()` ?
log.atInfo()
.setMessage("Loaded configuration '{}' from admin-db is: {}")
.addArgument(configId)
.addArgument(config)
.log();
}
boolean dbConfigExists = false;
config = tx.readFeatures(NakshaHubConfig.class, NakshaAdminCollection.CONFIGS)
.getFeatureById(configId);
if (config != null) {
dbConfigExists = true;
}
try (final Json json = Json.get()) {
final String configJson = IoHelp.readResource("config/local.json");
config = json.reader(ViewDeserialize.Storage.class)
.forType(NakshaHubConfig.class)
.readValue(configJson);
ModifyFeaturesReq<NakshaHubConfig> req = new ModifyFeaturesReq<>(false);
req = dbConfigExists ? req.update(config) : req.insert(config);
tx.writeFeatures(NakshaHubConfig.class, NakshaAdminCollection.CONFIGS)
.modifyFeatures(req);
tx.commit();
} catch (Exception e) {
throw unchecked(e);
}
// TODO HP_QUERY : Why this way instead of direct call `log.info()` ?
log.atInfo()
.setMessage("Loaded default configuration '{}' as: {}")
.addArgument(configId)
.addArgument(config)
.log();

// Read the configuration.
try (final Json json = Json.get()) {
Expand Down Expand Up @@ -404,6 +414,11 @@ public NakshaHub(@NotNull PsqlConfig adminDbConfig, @NotNull String configId, @N
return config;
}

/**
* The default storage object in use by Naksha.
*/
private final @NotNull Storage storage;

/**
* The admin storage.
*/
Expand Down Expand Up @@ -521,6 +536,8 @@ public void run() {
}
Thread.setDefaultUncaughtExceptionHandler(NakshaHub::uncaughtExceptionHandler);
Runtime.getRuntime().addShutdownHook(this.shutdownThread);
// Schedule backend Storage maintenance job
new StorageMaintainer(this.config, this.storage, this.adminStorage, this.txSettings).scheduleJob();
// TODO: Start metric publisher!
// startMetricPublishers();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,18 @@ public final class NakshaHubConfig extends XyzFeature implements JsonSerializabl
@JsonProperty("env") @Nullable String env,
@JsonProperty("webRoot") @Nullable String webRoot,
@JsonProperty("jwtName") @Nullable String jwtName,
@JsonProperty("debug") @Nullable Boolean debug) {
@JsonProperty("debug") @Nullable Boolean debug,
@JsonProperty("maintenanceIntervalInMins") @Nullable Integer maintenanceIntervalInMins,
@JsonProperty("maintenanceInitialDelayInMins") @Nullable Integer maintenanceInitialDelayInMins,
@JsonProperty("maintenancePoolCoreSize") @Nullable Integer maintenancePoolCoreSize,
@JsonProperty("maintenancePoolMaxSize") @Nullable Integer maintenancePoolMaxSize) {
super(id);
if (httpPort != null && (httpPort < 0 || httpPort > 65535)) {
currentLogger()
.atError("Invalid port in Naksha configuration: {}")
.add(httpPort)
.log();
// TODO HP_QUERY : Is it (temporarily) intentional to keep port always as 7080?
httpPort = 7080;
} else if (httpPort == null || httpPort == 0) {
httpPort = 7080;
Expand Down Expand Up @@ -134,6 +139,15 @@ public final class NakshaHubConfig extends XyzFeature implements JsonSerializabl
this.jwtName = jwtName != null && !jwtName.isEmpty() ? jwtName : "jwt";
this.userAgent = userAgent != null && !userAgent.isEmpty() ? userAgent : defaultAppName();
this.debug = Boolean.TRUE.equals(debug);
this.maintenanceIntervalInMins =
maintenanceIntervalInMins != null ? maintenanceIntervalInMins : defaultMaintenanceIntervalInMins();
this.maintenanceInitialDelayInMins = maintenanceInitialDelayInMins != null
? maintenanceInitialDelayInMins
: defaultMaintenanceInitialDelayInMins();
this.maintenancePoolCoreSize =
maintenancePoolCoreSize != null ? maintenancePoolCoreSize : defaultMaintenancePoolCoreSize();
this.maintenancePoolMaxSize =
maintenancePoolMaxSize != null ? maintenancePoolMaxSize : defaultMaintenancePoolMaxSize();
}

public static final String HTTP_PORT = "httpPort";
Expand Down Expand Up @@ -216,4 +230,56 @@ public final class NakshaHubConfig extends XyzFeature implements JsonSerializabl
@JsonProperty(DEBUG)
@JsonInclude(Include.NON_DEFAULT)
public boolean debug;

/**
* The initial delay (in minutes) after the service start up, when the Storage Maintenance job should perform first execution
*/
public final int maintenanceInitialDelayInMins;

/**
* Returns a default initial delay in mins, for starting Storage Maintenance job.
* @return The default interval
*/
public static int defaultMaintenanceInitialDelayInMins() {
return 1 * 60; // 1 hour
}

/**
* The interval in minutes, with which the Storage Maintenance job is to be scheduled
*/
public final int maintenanceIntervalInMins;

/**
* Returns a default interval in mins, for scheduling Storage Maintenance job.
* @return The default interval
*/
public static int defaultMaintenanceIntervalInMins() {
return 12 * 60; // 12 hours
}

/**
* The initial size of thread pool for running Storage Maintenance jobs in parallel
*/
public final int maintenancePoolCoreSize;

/**
* Returns a default initial size of thread pool for running Storage Maintenance jobs in parallel
* @return the default core size of maintenance thread pool
*/
public static int defaultMaintenancePoolCoreSize() {
return 5;
}

/**
* The maximum size of thread pool for running Storage Maintenance jobs in parallel
*/
public final int maintenancePoolMaxSize;

/**
* Returns a default maximum size of thread pool for running Storage Maintenance jobs in parallel
* @return the default max size of maintenance thread pool
*/
public static int defaultMaintenancePoolMaxSize() {
return 5;
}
}
Loading

0 comments on commit 4a07754

Please sign in to comment.