Search in sources :

Example 36 with Instance

use of javax.enterprise.inject.Instance in project stackgres by ongres.

the class DbOpLauncherImpl method launchDbOp.

@Override
public void launchDbOp(String dbOpName, String namespace) {
    StackGresDbOps dbOps = dbOpsFinder.findByNameAndNamespace(dbOpName, namespace).orElseThrow(() -> new IllegalArgumentException(StackGresDbOps.KIND + " " + dbOpName + " does not exists in namespace " + namespace));
    Instance<DatabaseOperationJob> jobImpl = instance.select(new DatabaseOperationLiteral(dbOps.getSpec().getOp()));
    if (jobImpl.isResolvable()) {
        LOGGER.info("Initializing conditions for SgDbOps {}", dbOps.getMetadata().getName());
        var status = Optional.ofNullable(dbOps.getStatus()).or(() -> Optional.of(new StackGresDbOpsStatus())).map(dbOpsStatus -> {
            dbOpsStatus.setOpStarted(Instant.now().toString());
            dbOpsStatus.setOpRetries(Optional.ofNullable(dbOpsStatus.getOpRetries()).map(opRetries -> opRetries + 1).orElse(0));
            dbOpsStatus.setConditions(getStartingConditions());
            return dbOpsStatus;
        }).orElseThrow();
        dbOps.setStatus(status);
        final StackGresDbOps initializedDbOps = dbOpsScheduler.update(dbOps);
        try {
            final int lockPollInterval = Integer.parseInt(DBOPS_LOCK_POLL_INTERVAL.getString());
            final int timeout = Integer.parseInt(DBOPS_LOCK_TIMEOUT.getString());
            LockRequest lockRequest = ImmutableLockRequest.builder().namespace(initializedDbOps.getMetadata().getNamespace()).serviceAccount(JobsProperty.SERVICE_ACCOUNT.getString()).podName(JobsProperty.POD_NAME.getString()).pollInterval(lockPollInterval).timeout(timeout).lockResourceName(initializedDbOps.getSpec().getSgCluster()).build();
            Infrastructure.setDroppedExceptionHandler(err -> LOGGER.error("Dropped exception ", err));
            lockAcquirer.lockRun(lockRequest, (targetCluster) -> {
                databaseOperationEventEmitter.operationStarted(dbOpName, namespace);
                final DatabaseOperationJob databaseOperationJob = jobImpl.get();
                Uni<ClusterRestartState> jobUni = databaseOperationJob.runJob(initializedDbOps, targetCluster);
                if (initializedDbOps.getSpec().getTimeout() != null) {
                    jobUni.await().atMost(Duration.parse(initializedDbOps.getSpec().getTimeout()));
                } else {
                    jobUni.await().indefinitely();
                }
                databaseOperationEventEmitter.operationCompleted(dbOpName, namespace);
            });
            LOGGER.info("Operation completed for SgDbOp {}", dbOpName);
            updateToCompletedConditions(dbOpName, namespace);
        } catch (TimeoutException timeoutEx) {
            updateToTimeoutConditions(dbOpName, namespace);
            databaseOperationEventEmitter.operationTimedOut(dbOpName, namespace);
            throw timeoutEx;
        } catch (Exception e) {
            updateToFailedConditions(dbOpName, namespace);
            databaseOperationEventEmitter.operationFailed(dbOpName, namespace);
            throw e;
        }
    } else if (jobImpl.isAmbiguous()) {
        throw new IllegalStateException("Multiple implementations of the operation " + dbOps.getSpec().getOp() + " found");
    } else {
        throw new IllegalStateException("Implementation of operation " + dbOps.getSpec().getOp() + " not found");
    }
}
Also used : LockRequest(io.stackgres.jobs.dbops.lock.LockRequest) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) LoggerFactory(org.slf4j.LoggerFactory) StackGresDbOpsCondition(io.stackgres.common.crd.sgdbops.StackGresDbOpsCondition) JobsProperty(io.stackgres.jobs.app.JobsProperty) LockAcquirer(io.stackgres.jobs.dbops.lock.LockAcquirer) StackGresDbOps(io.stackgres.common.crd.sgdbops.StackGresDbOps) Uni(io.smallrye.mutiny.Uni) Inject(javax.inject.Inject) Duration(java.time.Duration) StackGresDbOpsStatus(io.stackgres.common.crd.sgdbops.StackGresDbOpsStatus) CustomResourceFinder(io.stackgres.common.resource.CustomResourceFinder) Any(javax.enterprise.inject.Any) Instance(javax.enterprise.inject.Instance) Infrastructure(io.smallrye.mutiny.infrastructure.Infrastructure) Logger(org.slf4j.Logger) ImmutableLockRequest(io.stackgres.jobs.dbops.lock.ImmutableLockRequest) TimeoutException(io.smallrye.mutiny.TimeoutException) Instant(java.time.Instant) ClusterRestartState(io.stackgres.jobs.dbops.clusterrestart.ClusterRestartState) DBOPS_LOCK_TIMEOUT(io.stackgres.jobs.app.JobsProperty.DBOPS_LOCK_TIMEOUT) DbOpsStatusCondition(io.stackgres.common.crd.sgdbops.DbOpsStatusCondition) List(java.util.List) DBOPS_LOCK_POLL_INTERVAL(io.stackgres.jobs.app.JobsProperty.DBOPS_LOCK_POLL_INTERVAL) CustomResourceScheduler(io.stackgres.common.resource.CustomResourceScheduler) DateTimeFormatter(java.time.format.DateTimeFormatter) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) StackGresDbOps(io.stackgres.common.crd.sgdbops.StackGresDbOps) TimeoutException(io.smallrye.mutiny.TimeoutException) StackGresDbOpsStatus(io.stackgres.common.crd.sgdbops.StackGresDbOpsStatus) ClusterRestartState(io.stackgres.jobs.dbops.clusterrestart.ClusterRestartState) LockRequest(io.stackgres.jobs.dbops.lock.LockRequest) ImmutableLockRequest(io.stackgres.jobs.dbops.lock.ImmutableLockRequest) TimeoutException(io.smallrye.mutiny.TimeoutException)

Example 37 with Instance

use of javax.enterprise.inject.Instance in project stackgres by ongres.

the class ClusterDeployedResourceScanner method getDeployedResources.

@Override
public List<HasMetadata> getDeployedResources(StackGresCluster config) {
    try (KubernetesClient client = clientFactory.create()) {
        final Map<String, String> genericClusterLabels = labelFactory.genericLabels(config);
        Stream<HasMetadata> inNamespace = IN_NAMESPACE_RESOURCE_OPERATIONS.values().stream().flatMap(resourceOperationGetter -> resourceOperationGetter.apply(client).inNamespace(config.getMetadata().getNamespace()).withLabels(genericClusterLabels).list().getItems().stream());
        Stream<HasMetadata> anyNamespace = ANY_NAMESPACE_RESOURCE_OPERATIONS.values().stream().flatMap(resourceOperationGetter -> resourceOperationGetter.apply(client, genericClusterLabels).stream());
        List<HasMetadata> deployedResources = Stream.concat(inNamespace, anyNamespace).filter(resource1 -> resource1.getMetadata().getOwnerReferences().stream().anyMatch(ownerReference -> ownerReference.getKind().equals(StackGresCluster.KIND) && ownerReference.getName().equals(config.getMetadata().getName()) && ownerReference.getUid().equals(config.getMetadata().getUid()))).collect(Collectors.toUnmodifiableList());
        deployedResources.forEach(resource -> {
            Instance<DeployedResourceDecorator> decorator = decorators.select(new ReconciliationScopeLiteral(StackGresCluster.class, resource.getKind()));
            if (decorator.isResolvable()) {
                decorator.get().decorate(resource);
            }
        });
        return deployedResources;
    }
}
Also used : ReconciliationScopeLiteral(io.stackgres.operator.conciliation.ReconciliationScopeLiteral) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) ReconciliationOperations(io.stackgres.operator.conciliation.ReconciliationOperations) LabelFactoryForCluster(io.stackgres.common.LabelFactoryForCluster) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Inject(javax.inject.Inject) DeployedResourcesScanner(io.stackgres.operator.conciliation.DeployedResourcesScanner) List(java.util.List) Stream(java.util.stream.Stream) DeployedResourceDecorator(io.stackgres.operator.conciliation.DeployedResourceDecorator) Map(java.util.Map) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) KubernetesClientFactory(io.stackgres.common.KubernetesClientFactory) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Any(javax.enterprise.inject.Any) Instance(javax.enterprise.inject.Instance) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) ReconciliationScopeLiteral(io.stackgres.operator.conciliation.ReconciliationScopeLiteral) DeployedResourceDecorator(io.stackgres.operator.conciliation.DeployedResourceDecorator)

Example 38 with Instance

use of javax.enterprise.inject.Instance in project openwebbeans by apache.

the class OwbCDI method instance.

protected Instance<Object> instance() {
    WebBeansContext webBeansContext = getWebBeansContext();
    BeanManagerImpl bm = webBeansContext.getBeanManagerImpl();
    CreationalContextImpl<Instance<Object>> creationalContext = bm.createCreationalContext(null);
    return webBeansContext.getWebBeansUtil().getInstanceBean().create(creationalContext);
}
Also used : WebBeansContext(org.apache.webbeans.config.WebBeansContext) Instance(javax.enterprise.inject.Instance)

Example 39 with Instance

use of javax.enterprise.inject.Instance in project openwebbeans by apache.

the class TypeLiteralTest method testTypeLiteralInInstance.

@Test
public void testTypeLiteralInInstance() {
    startContainer(StringOrder.class, IntegerOrder.class, InstanceTypeLiteralBean.class);
    InstanceTypeLiteralBean beaninstance = getInstance(InstanceTypeLiteralBean.class);
    Object produce = beaninstance.produce(0);
    Assert.assertTrue(produce instanceof Instance);
    Instance<IntegerOrder> order = (Instance<IntegerOrder>) produce;
    Assert.assertTrue(order.get() instanceof IntegerOrder);
    produce = beaninstance.produce(1);
    Assert.assertTrue(produce instanceof Instance);
    Instance<StringOrder> order2 = (Instance<StringOrder>) produce;
    Assert.assertTrue(order2.get() instanceof StringOrder);
}
Also used : Instance(javax.enterprise.inject.Instance) InstanceTypeLiteralBean(org.apache.webbeans.test.component.literals.InstanceTypeLiteralBean) IntegerOrder(org.apache.webbeans.test.component.literals.InstanceTypeLiteralBean.IntegerOrder) StringOrder(org.apache.webbeans.test.component.literals.InstanceTypeLiteralBean.StringOrder) AbstractUnitTest(org.apache.webbeans.test.AbstractUnitTest) Test(org.junit.Test)

Aggregations

Instance (javax.enterprise.inject.Instance)39 Test (org.junit.Test)14 List (java.util.List)10 Inject (javax.inject.Inject)10 ArrayList (java.util.ArrayList)9 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)9 Map (java.util.Map)8 Optional (java.util.Optional)8 Collectors (java.util.stream.Collectors)6 Annotation (java.lang.annotation.Annotation)5 Stream (java.util.stream.Stream)4 InjectionPoint (javax.enterprise.inject.spi.InjectionPoint)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 Settings (com.artezio.arttime.config.Settings)3 Dl (eu.ggnet.dwoss.core.widget.Dl)3 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 JCommander (com.beust.jcommander.JCommander)2 ParameterException (com.beust.jcommander.ParameterException)2