All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.camunda.zeebe.broker.system.partitions.impl.steps.MetricsStep Maven / Gradle / Ivy

There is a newer version: 8.7.0-alpha1
Show newest version
/*
 * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH under
 * one or more contributor license agreements. See the NOTICE file distributed
 * with this work for additional information regarding copyright ownership.
 * Licensed under the Camunda License 1.0. You may not use this file
 * except in compliance with the Camunda License 1.0.
 */
package io.camunda.zeebe.broker.system.partitions.impl.steps;

import io.atomix.raft.RaftServer.Role;
import io.camunda.zeebe.broker.system.partitions.PartitionTransitionContext;
import io.camunda.zeebe.broker.system.partitions.PartitionTransitionStep;
import io.camunda.zeebe.scheduler.future.ActorFuture;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;

public final class MetricsStep implements PartitionTransitionStep {

  @Override
  public ActorFuture prepareTransition(
      final PartitionTransitionContext context, final long term, final Role targetRole) {
    final var partitionMeterRegistry = (CompositeMeterRegistry) context.getPartitionMeterRegistry();
    final var brokerMeterRegistry = context.getBrokerMeterRegistry();
    if (partitionMeterRegistry != null) {
      // Clear all meters from the partition registry. Their values are invalid after the
      // transition.
      partitionMeterRegistry.clear();
      // Remove the backing broker registry from the partition registry so that we can close the
      // partition registry without closing the broker registry.
      // This also increases reliability in case something holds on to the partition registry
      // because any new meters will no longer be forwarded to the broker registry.
      partitionMeterRegistry.remove(brokerMeterRegistry);
      partitionMeterRegistry.close();
      context.setPartitionMeterRegistry(null);
    }
    return context.getConcurrencyControl().createCompletedFuture();
  }

  @Override
  public ActorFuture transitionTo(
      final PartitionTransitionContext context, final long term, final Role targetRole) {
    final var brokerRegistry = context.getBrokerMeterRegistry();

    // Create a new registry that already has the partition tag defined.
    final var partitionRegistry = new CompositeMeterRegistry();
    partitionRegistry
        .config()
        .commonTags(Tags.of("partition", Integer.toString(context.getPartitionId())));
    // Wrap over the broker registry so that all meters are forwarded to the broker registry.
    partitionRegistry.add(brokerRegistry);

    context.setPartitionMeterRegistry(partitionRegistry);
    return context.getConcurrencyControl().createCompletedFuture();
  }

  @Override
  public String getName() {
    return "Metrics";
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy