public static synchronized void closeDataSource() { if (ds != null) { PostgresHostUtils.closeDataSource(ds); ds = null; } }
public static DataSource createDataSource() { Properties props = getDataSourceProperties(); return createDataSource(props); }
@Override public void replicateRequest(EnumSet<Service.ServiceOption> serviceOptions, ServiceDocument state, String selectorPath, String selectionKey, Operation op) { if (this.enablePostgres && PostgresHostUtils.handleReplicateRequest(this, serviceOptions, state, selectorPath, selectionKey, op)) { return; } super.replicateRequest(serviceOptions, state, selectorPath, selectionKey, op); }
Properties props = PostgresHostUtils.getDataSourceProperties(); props.setProperty("dataSource.databaseName", databaseName); props.setProperty("minimumIdle", "0"); DataSource ds = PostgresHostUtils.createDataSource(props); try { if (createDatabase) { throw new IllegalStateException(e); } finally { PostgresHostUtils.closeDataSource(ds);
@Override public void registerForServiceAvailability(Operation.CompletionHandler completion, String nodeSelectorPath, boolean checkReplica, String... servicePaths) { if (this.enablePostgres && checkReplica) { PostgresHostUtils.registerForReplicatedServiceAvailability(this, completion, isStarted() ? getSystemAuthorizationContext() : null, nodeSelectorPath, servicePaths); return; } super.registerForServiceAvailability(completion, nodeSelectorPath, checkReplica, servicePaths); }
public static void handleServiceStart(ServiceHost host, Operation op, Service service, PostgresDocumentIndexService indexService) { handleServiceStart(host, op, service, indexService, true); }
public static void registerForReplicatedServiceAvailability(ServiceHost host, Operation op, String servicePath, String nodeSelectorPath, AuthorizationContext authorizationContext) { CompletionHandler ch = (o, e) -> { if (e != null) { if (op.getExpirationMicrosUtc() < Utils.getSystemNowMicrosUtc()) { String msg = "Failed to check replicated service availability"; op.fail(new TimeoutException(msg)); return; } // service is not yet available, reschedule host.scheduleCore(() -> { registerForReplicatedServiceAvailability(host, op, servicePath, nodeSelectorPath, authorizationContext); }, host.getMaintenanceIntervalMicros(), TimeUnit.MICROSECONDS); return; } op.complete(); }; URI serviceUri = UriUtils.buildUri(host, servicePath); checkReplicatedServiceAvailability(ch, host, serviceUri, nodeSelectorPath, authorizationContext); }
@Override protected void startDocumentIndexService(Service documentIndexService, NodeSelectorService defaultNodeSelectorService) throws Throwable { if (!this.enablePostgres) { super.startDocumentIndexService(documentIndexService, defaultNodeSelectorService); return; } PostgresHostUtils.handleStartDocumentIndexService(this, documentIndexService, defaultNodeSelectorService, this::startCoreServicesSynchronously); }
@Override public void sendRequest(Operation op) { if (this.enablePostgres && PostgresHostUtils.handleSendRequest(this, op, this::findService)) { return; } super.sendRequest(op); }
@Override public boolean handleRequest(Service service, Operation inboundOp) { if (inboundOp == null && service != null) { inboundOp = service.dequeueRequest(); } if (inboundOp == null) { return true; } // Reject remote requests with 503 if node is not started, node stopping or // isRejectRemoteRequests flag is true (which can be used to reject requests before starting // factories) if ((!isStarted() || isStopping() || this.isRejectRemoteRequests) && inboundOp.isRemote()) { PostgresHostUtils.failServerNotAvailable(inboundOp); return true; } return super.handleRequest(null, inboundOp); }
PostgresHostUtils.clearCacheOnNodeGroupChange(this);
protected DataSource getDataSource() { if (this.ds == null) { this.ds = PostgresHostUtils.createDataSource(); } return this.ds; }
public static void registerForReplicatedServiceAvailability(ServiceHost host, CompletionHandler completion, AuthorizationContext authorizationContext, String nodeSelectorPath, String... servicePaths) { for (String link : servicePaths) { Operation op = Operation.createPost(host, link) .setCompletion(completion) .setExpiration(Utils.fromNowMicrosUtc(host.getOperationTimeoutMicros())); registerForReplicatedServiceAvailability(host, op, link, nodeSelectorPath, authorizationContext); } }
@Override protected ServiceHost startService(Operation post, Service service, Operation onDemandTriggeringOp) { if (this.enablePostgres) { boolean registerFactory = !enableRegisterPostgresSchema || createTableOnFactoryStart; PostgresHostUtils .handleServiceStart(this, post, service, this.postgresDocumentIndexService, registerFactory); } return super.startService(post, service, onDemandTriggeringOp); }
@Override protected void startDocumentIndexService(Service documentIndexService, NodeSelectorService defaultNodeSelectorService) throws Throwable { if (!enablePostgres) { super.startDocumentIndexService(documentIndexService, defaultNodeSelectorService); return; } PostgresHostUtils.handleStartDocumentIndexService(this, documentIndexService, defaultNodeSelectorService, this::startCoreServicesSynchronously); }
@Override public void sendRequest(Operation op) { if (enablePostgres && PostgresHostUtils.handleSendRequest(this, op, this::findService)) { return; } super.sendRequest(op); }
@Override public boolean handleRequest(Service service, Operation inboundOp) { if (inboundOp == null && service != null) { inboundOp = service.dequeueRequest(); } if (inboundOp == null) { return true; } // Reject remote requests with 503 if node is not started, node stopping or // isRejectRemoteRequests flag is true (which can be used to reject requests before starting // factories) if ((!isStarted() || isStopping() || this.isRejectRemoteRequests) && inboundOp.isRemote()) { PostgresHostUtils.failServerNotAvailable(inboundOp); return true; } return super.handleRequest(null, inboundOp); }
@Override public ServiceHost start() throws Throwable { if (!enablePostgres) { return super.start(); } // disable synchronization setPeerSynchronizationEnabled(false); super.start(); if (this.enableClearCacheOnNodeGroupChange) { // Use system context OperationContext origContext = OperationContext.getOperationContext(); setAuthorizationContext(getSystemAuthorizationContext()); // Subscribe for node group change and clear cache on changes PostgresHostUtils.clearCacheOnNodeGroupChange(this); // Restore context OperationContext.setFrom(origContext); } return this; }
public static synchronized DataSource getDataSource() { if (ds == null) { Properties props = PostgresHostUtils.getDataSourceProperties(); props.setProperty("minimumIdle", "0"); ds = PostgresHostUtils.createDataSource(props); } return ds; }
@Override public void registerForServiceAvailability(Operation.CompletionHandler completion, String nodeSelectorPath, boolean checkReplica, String... servicePaths) { log("servicePaths %s: %s", checkReplica, Utils.toJson(servicePaths)); if (enablePostgres && checkReplica) { PostgresHostUtils.registerForReplicatedServiceAvailability(this, completion, isStarted() ? getSystemAuthorizationContext() : null, nodeSelectorPath, servicePaths); return; } super.registerForServiceAvailability(completion, nodeSelectorPath, checkReplica, servicePaths); }