Search in sources :

Example 1 with Profiler

use of org.slf4j.profiler.Profiler in project testcontainers-java by testcontainers.

the class GenericContainer method start.

/**
 * Starts the container using docker, pulling an image if necessary.
 */
public void start() {
    Profiler profiler = new Profiler("Container startup");
    profiler.setLogger(logger());
    try {
        profiler.start("Prepare container configuration and host configuration");
        configure();
        logger().debug("Starting container: {}", getDockerImageName());
        logger().debug("Trying to start container: {}", image.get());
        AtomicInteger attempt = new AtomicInteger(0);
        Unreliables.retryUntilSuccess(startupAttempts, () -> {
            logger().debug("Trying to start container: {} (attempt {}/{})", image.get(), attempt.incrementAndGet(), startupAttempts);
            tryStart(profiler.startNested("Container startup attempt"));
            return true;
        });
    } catch (Exception e) {
        throw new ContainerLaunchException("Container startup failed", e);
    } finally {
        profiler.stop().log();
    }
}
Also used : Profiler(org.slf4j.profiler.Profiler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IOException(java.io.IOException)

Example 2 with Profiler

use of org.slf4j.profiler.Profiler in project openlmis-stockmanagement by OpenLMIS.

the class StockCardSummariesV2Controller method getStockCardSummaries.

/**
 * Get stock card summaries by program and facility.
 *
 * @return Stock card summaries.
 */
@GetMapping
public Page<StockCardSummaryV2Dto> getStockCardSummaries(@RequestParam MultiValueMap<String, String> parameters, @PageableDefault(size = Integer.MAX_VALUE) Pageable pageable) {
    Profiler profiler = new Profiler("GET_STOCK_CARDS_V2");
    profiler.setLogger(LOGGER);
    profiler.start("VALIDATE_PARAMS");
    StockCardSummariesV2SearchParams params = new StockCardSummariesV2SearchParams(parameters);
    profiler.start("GET_STOCK_CARD_SUMMARIES");
    StockCardSummaries summaries = stockCardSummariesService.findStockCards(params);
    profiler.start("TO_DTO");
    List<StockCardSummaryV2Dto> dtos = stockCardSummariesV2DtoBuilder.build(summaries.getPageOfApprovedProducts(), summaries.getStockCardsForFulfillOrderables(), summaries.getOrderableFulfillMap(), summaries.getAsOfDate());
    profiler.start("GET_PAGE");
    Page<StockCardSummaryV2Dto> page = Pagination.getPage(dtos, pageable);
    profiler.stop().log();
    return page;
}
Also used : Profiler(org.slf4j.profiler.Profiler) StockCardSummariesV2SearchParams(org.openlmis.stockmanagement.service.StockCardSummariesV2SearchParams) StockCardSummaries(org.openlmis.stockmanagement.service.StockCardSummaries) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 3 with Profiler

use of org.slf4j.profiler.Profiler in project openlmis-stockmanagement by OpenLMIS.

the class StockEventProcessContextBuilder method buildContext.

/**
 * Before processing events, put all needed ref data into context so we don't have to do frequent
 * network requests.
 *
 * @param eventDto event dto.
 * @return a context object that includes all needed ref data.
 */
public StockEventProcessContext buildContext(StockEventDto eventDto) {
    XLOGGER.entry(eventDto);
    Profiler profiler = new Profiler("BUILD_CONTEXT");
    profiler.setLogger(XLOGGER);
    LOGGER.info("build stock event process context");
    StockEventProcessContext context = new StockEventProcessContext();
    profiler.start("CREATE_LAZY_USER");
    OAuth2Authentication authentication = (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication();
    Supplier<UUID> userIdSupplier;
    if (authentication.isClientOnly()) {
        userIdSupplier = eventDto::getUserId;
    } else {
        userIdSupplier = () -> authenticationHelper.getCurrentUser().getId();
    }
    LazyResource<UUID> userId = new LazyResource<>(userIdSupplier);
    context.setCurrentUserId(userId);
    profiler.start("CREATE_LAZY_PROGRAM");
    UUID programId = eventDto.getProgramId();
    Supplier<ProgramDto> programSupplier = new ReferenceDataSupplier<>(programService, programId);
    LazyResource<ProgramDto> program = new LazyResource<>(programSupplier);
    context.setProgram(program);
    profiler.start("CREATE_LAZY_FACILITY");
    UUID facilityId = eventDto.getFacilityId();
    Supplier<FacilityDto> facilitySupplier = new ReferenceDataSupplier<>(facilityService, facilityId);
    LazyResource<FacilityDto> facility = new LazyResource<>(facilitySupplier);
    context.setFacility(facility);
    profiler.start("CREATE_LAZY_APPROVED_PRODUCTS");
    Supplier<List<OrderableDto>> productsSupplier = () -> orderableReferenceDataService.findAll();
    LazyList<OrderableDto> products = new LazyList<>(productsSupplier);
    context.setAllApprovedProducts(products);
    profiler.start("CREATE_LAZY_LOTS");
    Supplier<List<LotDto>> lotsSupplier = () -> getLots(eventDto);
    LazyList<LotDto> lots = new LazyList<>(lotsSupplier);
    LazyGrouping<UUID, LotDto> lotsGroupedById = new LazyGrouping<>(lots, LotDto::getId);
    context.setLots(lotsGroupedById);
    profiler.start("CREATE_LAZY_EVENT_REASONS");
    Supplier<List<StockCardLineItemReason>> eventReasonsSupplier = () -> reasonRepository.findByIdIn(eventDto.getReasonIds());
    LazyList<StockCardLineItemReason> eventReasons = new LazyList<>(eventReasonsSupplier);
    LazyGrouping<UUID, StockCardLineItemReason> eventReasonsGroupedById = new LazyGrouping<>(eventReasons, StockCardLineItemReason::getId);
    context.setEventReasons(eventReasonsGroupedById);
    profiler.start("CREATE_LAZY_NODES");
    Supplier<List<Node>> nodesSupplier = () -> nodeRepository.findByIdIn(eventDto.getNodeIds());
    LazyList<Node> nodes = new LazyList<>(nodesSupplier);
    LazyGrouping<UUID, Node> nodesGroupedById = new LazyGrouping<>(nodes, Node::getId);
    context.setNodes(nodesGroupedById);
    profiler.start("CREATE_LAZY_STOCK_CARDS");
    Supplier<List<StockCard>> cardsSupplier = () -> stockCardRepository.findByProgramIdAndFacilityId(eventDto.getProgramId(), eventDto.getFacilityId());
    LazyList<StockCard> cards = new LazyList<>(cardsSupplier);
    LazyGrouping<OrderableLotIdentity, StockCard> cardsGroupedByIdentity = new LazyGrouping<>(cards, OrderableLotIdentity::identityOf);
    context.setCards(cardsGroupedByIdentity);
    profiler.start("CREATE_LAZY_CARD_REASONS");
    Supplier<List<StockCardLineItemReason>> cardReasonsSupplier = () -> getCardReasons(eventDto);
    LazyList<StockCardLineItemReason> cardReasons = new LazyList<>(cardReasonsSupplier);
    LazyGrouping<UUID, StockCardLineItemReason> cardReasonsGroupedById = new LazyGrouping<>(cardReasons, StockCardLineItemReason::getId);
    context.setCardReasons(cardReasonsGroupedById);
    profiler.start("CREATE_LAZY_SOURCES");
    Supplier<List<ValidSourceAssignment>> sourcesSupplier = () -> validSourceAssignmentRepository.findByProgramIdAndFacilityTypeId(eventDto.getProgramId(), context.getFacilityTypeId());
    LazyList<ValidSourceAssignment> sources = new LazyList<>(sourcesSupplier);
    context.setSources(sources);
    profiler.start("CREATE_LAZY_DESTINATIONS");
    Supplier<List<ValidDestinationAssignment>> destinationsSupplier = () -> validDestinationAssignmentRepository.findByProgramIdAndFacilityTypeId(eventDto.getProgramId(), context.getFacilityTypeId());
    LazyList<ValidDestinationAssignment> destinations = new LazyList<>(destinationsSupplier);
    context.setDestinations(destinations);
    profiler.stop().log();
    XLOGGER.exit(context);
    return context;
}
Also used : OrderableDto(org.openlmis.stockmanagement.dto.referencedata.OrderableDto) LazyList(org.openlmis.stockmanagement.util.LazyList) LazyResource(org.openlmis.stockmanagement.util.LazyResource) Node(org.openlmis.stockmanagement.domain.sourcedestination.Node) FacilityDto(org.openlmis.stockmanagement.dto.referencedata.FacilityDto) ValidSourceAssignment(org.openlmis.stockmanagement.domain.sourcedestination.ValidSourceAssignment) ValidDestinationAssignment(org.openlmis.stockmanagement.domain.sourcedestination.ValidDestinationAssignment) Profiler(org.slf4j.profiler.Profiler) ProgramDto(org.openlmis.stockmanagement.dto.referencedata.ProgramDto) StockCard(org.openlmis.stockmanagement.domain.card.StockCard) LazyList(org.openlmis.stockmanagement.util.LazyList) List(java.util.List) UUID(java.util.UUID) StockEventProcessContext(org.openlmis.stockmanagement.util.StockEventProcessContext) ReferenceDataSupplier(org.openlmis.stockmanagement.util.ReferenceDataSupplier) LotDto(org.openlmis.stockmanagement.dto.referencedata.LotDto) LazyGrouping(org.openlmis.stockmanagement.util.LazyGrouping) StockCardLineItemReason(org.openlmis.stockmanagement.domain.reason.StockCardLineItemReason) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) OrderableLotIdentity(org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity)

Example 4 with Profiler

use of org.slf4j.profiler.Profiler in project testcontainers-java by testcontainers.

the class LocalDockerCompose method starting.

@Override
@VisibleForTesting
public void starting(Description description) {
    final Profiler profiler = new Profiler("Docker Compose container rule");
    profiler.setLogger(logger());
    profiler.start("Docker Compose container startup");
    synchronized (MUTEX) {
        registerContainersForShutdown();
        if (pull) {
            pullImages();
        }
        // scale before up, so that all scaled instances are available first for linking
        applyScaling();
        createServices();
        startAmbassadorContainers(profiler);
        waitUntilServiceStarted();
    }
}
Also used : Profiler(org.slf4j.profiler.Profiler) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 5 with Profiler

use of org.slf4j.profiler.Profiler in project testcontainers-java by testcontainers.

the class ImageFromDockerfile method resolve.

@Override
protected final String resolve() {
    Logger logger = DockerLoggerFactory.getLogger(dockerImageName);
    Profiler profiler = new Profiler("Rule creation - build image");
    profiler.setLogger(logger);
    DockerClient dockerClient = DockerClientFactory.instance().client();
    try {
        if (deleteOnExit) {
            imagesToDelete.add(dockerImageName);
        }
        BuildImageResultCallback resultCallback = new BuildImageResultCallback() {

            @Override
            public void onNext(BuildResponseItem item) {
                super.onNext(item);
                if (item.isErrorIndicated()) {
                    logger.error(item.getErrorDetail().getMessage());
                } else {
                    logger.debug(StringUtils.chomp(item.getStream(), "\n"));
                }
            }
        };
        // We have to use pipes to avoid high memory consumption since users might want to build really big images
        @Cleanup PipedInputStream in = new PipedInputStream();
        @Cleanup PipedOutputStream out = new PipedOutputStream(in);
        profiler.start("Configure image");
        BuildImageCmd buildImageCmd = dockerClient.buildImageCmd(in);
        configure(buildImageCmd);
        profiler.start("Build image");
        BuildImageResultCallback exec = buildImageCmd.exec(resultCallback);
        // To build an image, we have to send the context to Docker in TAR archive format
        profiler.start("Send context as TAR");
        try (TarArchiveOutputStream tarArchive = new TarArchiveOutputStream(new GZIPOutputStream(out))) {
            tarArchive.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
            for (Map.Entry<String, Transferable> entry : transferables.entrySet()) {
                Transferable transferable = entry.getValue();
                final String destination = entry.getKey();
                transferable.transferTo(tarArchive, destination);
            }
            tarArchive.finish();
        }
        profiler.start("Wait for an image id");
        exec.awaitImageId();
        return dockerImageName;
    } catch (IOException e) {
        throw new RuntimeException("Can't close DockerClient", e);
    } finally {
        profiler.stop().log();
    }
}
Also used : BuildResponseItem(com.github.dockerjava.api.model.BuildResponseItem) DockerClient(com.github.dockerjava.api.DockerClient) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) Logger(org.slf4j.Logger) Cleanup(lombok.Cleanup) BuildImageResultCallback(com.github.dockerjava.core.command.BuildImageResultCallback) Profiler(org.slf4j.profiler.Profiler) GZIPOutputStream(java.util.zip.GZIPOutputStream) BuildImageCmd(com.github.dockerjava.api.command.BuildImageCmd) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Profiler (org.slf4j.profiler.Profiler)9 UUID (java.util.UUID)3 DockerClient (com.github.dockerjava.api.DockerClient)2 IOException (java.io.IOException)2 StockCard (org.openlmis.stockmanagement.domain.card.StockCard)2 OrderableLotIdentity (org.openlmis.stockmanagement.domain.identity.OrderableLotIdentity)2 StockCardLineItemReason (org.openlmis.stockmanagement.domain.reason.StockCardLineItemReason)2 StockEventProcessContext (org.openlmis.stockmanagement.util.StockEventProcessContext)2 Logger (org.slf4j.Logger)2 BuildImageCmd (com.github.dockerjava.api.command.BuildImageCmd)1 ListImagesCmd (com.github.dockerjava.api.command.ListImagesCmd)1 DockerClientException (com.github.dockerjava.api.exception.DockerClientException)1 BuildResponseItem (com.github.dockerjava.api.model.BuildResponseItem)1 Image (com.github.dockerjava.api.model.Image)1 BuildImageResultCallback (com.github.dockerjava.core.command.BuildImageResultCallback)1 PullImageResultCallback (com.github.dockerjava.core.command.PullImageResultCallback)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 HashMap (java.util.HashMap)1