Search in sources :

Example 6 with Payload

use of com.sequenceiq.cloudbreak.common.event.Payload in project cloudbreak by hortonworks.

the class StackStopRestartAction method restart.

@Override
public void restart(FlowParameters flowParameters, String flowChainId, String event, Object payload) {
    Payload stackPayload = (Payload) payload;
    Stack stack = stackService.getById(stackPayload.getResourceId());
    MDCBuilder.buildMdcContext(stack);
    super.restart(flowParameters, flowChainId, event, payload);
}
Also used : Payload(com.sequenceiq.cloudbreak.common.event.Payload) Stack(com.sequenceiq.cloudbreak.domain.stack.Stack)

Example 7 with Payload

use of com.sequenceiq.cloudbreak.common.event.Payload in project cloudbreak by hortonworks.

the class Flow2Handler method handleFlowConflict.

private AcceptResult handleFlowConflict(String key, Payload payload, String flowChainId, Set<FlowLogIdWithTypeAndTimestamp> flowLogItems) {
    AcceptResult acceptResult = null;
    Optional<FlowLog> initFlowLog = flowLogService.findAllByFlowIdOrderByCreatedDesc(flowLogItems.iterator().next().getFlowId()).stream().min(Comparator.comparing(FlowLog::getCreated));
    if (initFlowLog.isPresent()) {
        LOGGER.info("Found previous init flow log: {}", initFlowLog.get());
        if (NullUtil.allNotNull(initFlowLog.get().getFlowChainId(), flowChainId)) {
            Optional<Pair<String, Payload>> previousTrigger = flowChains.getRootTriggerEvent(initFlowLog.get().getFlowChainId());
            Optional<Pair<String, Payload>> currentTrigger = flowChains.getRootTriggerEvent(flowChainId);
            if (previousTrigger.isPresent() && currentTrigger.isPresent()) {
                if (isIdempotentTriggers(previousTrigger.get().getRight(), currentTrigger.get().getRight())) {
                    LOGGER.info("Idempotent flow chain trigger. Running {}, requested {}", previousTrigger, currentTrigger);
                    acceptResult = FlowAcceptResult.runningInFlowChain(previousTrigger.get().getLeft());
                }
            }
        } else if (NullUtil.allNull(initFlowLog.get().getFlowChainId(), flowChainId)) {
            Payload previousTrigger = FlowLogUtil.tryDeserializePayload(initFlowLog.get());
            if (isIdempotentTriggers(previousTrigger, payload)) {
                LOGGER.info("Idempotent flow trigger. Running {}, requested {}", previousTrigger, payload);
                acceptResult = FlowAcceptResult.runningInFlow(initFlowLog.get().getFlowId());
            }
        }
    }
    if (acceptResult == null) {
        LOGGER.info("Flow operation not allowed, other flow is running. Resource ID {}, event {}", payload.getResourceId(), key);
        acceptResult = FlowAcceptResult.alreadyExistingFlow(flowLogItems);
    }
    flowChains.removeFullFlowChain(flowChainId, false);
    return acceptResult;
}
Also used : FlowLog(com.sequenceiq.flow.domain.FlowLog) Payload(com.sequenceiq.cloudbreak.common.event.Payload) AcceptResult(com.sequenceiq.cloudbreak.common.event.AcceptResult) FlowAcceptResult(com.sequenceiq.flow.core.model.FlowAcceptResult) Pair(org.apache.commons.lang3.tuple.Pair)

Example 8 with Payload

use of com.sequenceiq.cloudbreak.common.event.Payload in project cloudbreak by hortonworks.

the class Flow2Handler method accept.

@Override
public void accept(Event<? extends Payload> event) {
    String key = (String) event.getKey();
    Payload payload = event.getData();
    String flowId = getFlowId(event);
    String flowChainId = getFlowChainId(event);
    String flowChainType = getFlowChainType(event);
    String flowTriggerUserCrn = getFlowTriggerUserCrn(event);
    String operationType = getFlowOperationType(event);
    Span activeSpan = tracer.activeSpan();
    SpanContext spanContext = event.getHeaders().get(FlowConstants.SPAN_CONTEXT);
    String operationName = event.getKey().toString();
    if (FlowTracingUtil.isActiveSpanReusable(activeSpan, spanContext, operationName)) {
        LOGGER.debug("Reusing existing span. {}", activeSpan.context());
        doAccept(event, key, payload, flowChainId, flowChainType, new FlowParameters(flowId, flowTriggerUserCrn, operationType, spanContext));
    } else {
        Span span = FlowTracingUtil.getSpan(tracer, operationName, spanContext, flowId, flowChainId, flowTriggerUserCrn);
        spanContext = FlowTracingUtil.useOrCreateSpanContext(spanContext, span);
        try (Scope ignored = tracer.activateSpan(span)) {
            doAccept(event, key, payload, flowChainId, flowChainType, new FlowParameters(flowId, flowTriggerUserCrn, operationType, spanContext));
        } finally {
            span.finish();
        }
    }
}
Also used : SpanContext(io.opentracing.SpanContext) Scope(io.opentracing.Scope) Payload(com.sequenceiq.cloudbreak.common.event.Payload) Span(io.opentracing.Span)

Example 9 with Payload

use of com.sequenceiq.cloudbreak.common.event.Payload in project cloudbreak by hortonworks.

the class FlowChainHandler method restoreFlowChain.

public void restoreFlowChain(String flowChainId) {
    Optional<FlowChainLog> chainLog = flowLogService.findFirstByFlowChainIdOrderByCreatedDesc(flowChainId);
    if (chainLog.isPresent()) {
        String flowChainType = chainLog.get().getFlowChainType();
        Queue<Selectable> queue = (Queue<Selectable>) JsonReader.jsonToJava(chainLog.get().getChain());
        Payload triggerEvent = tryDeserializeTriggerEvent(chainLog.get());
        FlowTriggerEventQueue chain = new FlowTriggerEventQueue(flowChainType, triggerEvent, queue);
        if (chainLog.get().getParentFlowChainId() != null) {
            chain.setParentFlowChainId(chainLog.get().getParentFlowChainId());
        }
        flowChains.putFlowChain(flowChainId, chainLog.get().getParentFlowChainId(), chain);
        Selectable selectable = queue.peek();
        if (selectable != null) {
            OperationType operationType = flowChainOperationTypeConfig.getFlowTypeOperationTypeMap().getOrDefault(flowChainType, OperationType.UNKNOWN);
            flowStatCache.putByFlowChainId(flowChainId, selectable.getResourceId(), operationType.name(), true);
        }
        if (chainLog.get().getParentFlowChainId() != null) {
            restoreFlowChain(chainLog.get().getParentFlowChainId());
        }
    }
}
Also used : FlowTriggerEventQueue(com.sequenceiq.flow.core.chain.config.FlowTriggerEventQueue) Selectable(com.sequenceiq.cloudbreak.common.event.Selectable) Payload(com.sequenceiq.cloudbreak.common.event.Payload) OperationType(com.sequenceiq.flow.api.model.operation.OperationType) FlowChainLog(com.sequenceiq.flow.domain.FlowChainLog) FlowTriggerEventQueue(com.sequenceiq.flow.core.chain.config.FlowTriggerEventQueue) Queue(java.util.Queue)

Example 10 with Payload

use of com.sequenceiq.cloudbreak.common.event.Payload in project cloudbreak by hortonworks.

the class ExceptionCatcherEventHandlerTest method testAcceptWhenSuccessful.

@Test
public void testAcceptWhenSuccessful() {
    Event<Payload> event = mock(Event.class);
    Event.Headers headers = mock(Event.Headers.class);
    Selectable sendEventObject = mock(Selectable.class);
    underTest.initialize(SELECTOR, null, false, sendEventObject);
    when(event.getHeaders()).thenReturn(headers);
    when(sendEventObject.selector()).thenReturn(SELECTOR);
    underTest.accept(event);
    ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
    verify(eventBus).notify(eq(SELECTOR), eventCaptor.capture());
    Event eventBusEvent = eventCaptor.getValue();
    assertNotNull(eventBusEvent);
    assertEquals(sendEventObject, eventBusEvent.getData());
}
Also used : Selectable(com.sequenceiq.cloudbreak.common.event.Selectable) Event(reactor.bus.Event) Payload(com.sequenceiq.cloudbreak.common.event.Payload) Test(org.junit.jupiter.api.Test)

Aggregations

Payload (com.sequenceiq.cloudbreak.common.event.Payload)36 Test (org.junit.jupiter.api.Test)11 Stack (com.sequenceiq.cloudbreak.domain.stack.Stack)10 Optional (java.util.Optional)10 Inject (javax.inject.Inject)10 Event (reactor.bus.Event)10 Selectable (com.sequenceiq.cloudbreak.common.event.Selectable)9 MDCBuilder (com.sequenceiq.cloudbreak.logger.MDCBuilder)8 List (java.util.List)8 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)7 AvailabilityZone.availabilityZone (com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone.availabilityZone)7 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)7 Location (com.sequenceiq.cloudbreak.cloud.model.Location)7 Location.location (com.sequenceiq.cloudbreak.cloud.model.Location.location)7 Region.region (com.sequenceiq.cloudbreak.cloud.model.Region.region)7 FlowParameters (com.sequenceiq.flow.core.FlowParameters)7 Map (java.util.Map)7 Collectors (java.util.stream.Collectors)7 StateContext (org.springframework.statemachine.StateContext)7 Crn (com.sequenceiq.cloudbreak.auth.crn.Crn)6