-
Notifications
You must be signed in to change notification settings - Fork 556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: don't fail on startup if disk usage monitor is disabled #10749
Changes from 3 commits
6c527c2
f6c2fdc
1a88687
aafcdef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,12 +7,16 @@ | |
*/ | ||
package io.camunda.zeebe.broker.bootstrap; | ||
|
||
import io.camunda.zeebe.broker.system.configuration.DataCfg; | ||
import io.camunda.zeebe.broker.system.monitoring.DiskSpaceUsageListener; | ||
import io.camunda.zeebe.broker.system.monitoring.DiskSpaceUsageMonitor; | ||
import io.camunda.zeebe.broker.system.monitoring.DiskSpaceUsageMonitorActor; | ||
import io.camunda.zeebe.scheduler.ConcurrencyControl; | ||
import io.camunda.zeebe.scheduler.future.ActorFuture; | ||
import io.camunda.zeebe.util.FileUtil; | ||
import java.io.IOException; | ||
import java.nio.file.Paths; | ||
import java.util.function.LongSupplier; | ||
|
||
class DiskSpaceUsageMonitorStep extends AbstractBrokerStartupStep { | ||
|
||
|
@@ -23,14 +27,43 @@ void startupInternal( | |
final ActorFuture<BrokerStartupContext> startupFuture) { | ||
|
||
final var data = brokerStartupContext.getBrokerConfiguration().getData(); | ||
|
||
if (!data.isDiskUsageMonitoringEnabled()) { | ||
brokerStartupContext.setDiskSpaceUsageMonitor(new DisabledDiskUsageMonitor()); | ||
startupFuture.complete(brokerStartupContext); | ||
return; | ||
} | ||
|
||
startDiskUsageMonitorActor(brokerStartupContext, startupFuture, data); | ||
} | ||
|
||
@Override | ||
void shutdownInternal( | ||
final BrokerStartupContext brokerShutdownContext, | ||
final ConcurrencyControl concurrencyControl, | ||
final ActorFuture<BrokerStartupContext> shutdownFuture) { | ||
|
||
final var diskSpaceUsageMonitor = brokerShutdownContext.getDiskSpaceUsageMonitor(); | ||
if (diskSpaceUsageMonitor instanceof DiskSpaceUsageMonitorActor actor) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔧 It would be better to add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, good point 👍 I've just added an |
||
stopDiskUsageMonitorActor(brokerShutdownContext, concurrencyControl, shutdownFuture, actor); | ||
} else { | ||
brokerShutdownContext.setDiskSpaceUsageMonitor(null); | ||
shutdownFuture.complete(brokerShutdownContext); | ||
} | ||
} | ||
|
||
private static void startDiskUsageMonitorActor( | ||
final BrokerStartupContext brokerStartupContext, | ||
final ActorFuture<BrokerStartupContext> startupFuture, | ||
final DataCfg data) { | ||
try { | ||
FileUtil.ensureDirectoryExists(Paths.get(data.getDirectory())); | ||
} catch (final IOException e) { | ||
startupFuture.completeExceptionally(e); | ||
return; | ||
} | ||
|
||
final var diskSpaceUsageMonitor = new DiskSpaceUsageMonitor(data); | ||
final var diskSpaceUsageMonitor = new DiskSpaceUsageMonitorActor(data); | ||
|
||
final var actorStartFuture = | ||
brokerStartupContext.getActorSchedulingService().submitActor(diskSpaceUsageMonitor); | ||
|
@@ -50,43 +83,48 @@ void startupInternal( | |
}); | ||
} | ||
|
||
@Override | ||
void shutdownInternal( | ||
private void stopDiskUsageMonitorActor( | ||
final BrokerStartupContext brokerShutdownContext, | ||
final ConcurrencyControl concurrencyControl, | ||
final ActorFuture<BrokerStartupContext> shutdownFuture) { | ||
|
||
final var diskSpaceUsageMonitor = brokerShutdownContext.getDiskSpaceUsageMonitor(); | ||
final ActorFuture<BrokerStartupContext> shutdownFuture, | ||
final DiskSpaceUsageMonitorActor actor) { | ||
final var closeFuture = actor.closeAsync(); | ||
concurrencyControl.runOnCompletion( | ||
closeFuture, | ||
(ok, error) -> { | ||
if (error != null) { | ||
shutdownFuture.completeExceptionally(error); | ||
return; | ||
} | ||
|
||
if (diskSpaceUsageMonitor != null) { | ||
final var closeFuture = diskSpaceUsageMonitor.closeAsync(); | ||
concurrencyControl.runOnCompletion( | ||
closeFuture, | ||
(ok, error) -> { | ||
if (error != null) { | ||
shutdownFuture.completeExceptionally(error); | ||
return; | ||
} | ||
|
||
forwardExceptions( | ||
() -> | ||
concurrencyControl.run( | ||
() -> | ||
forwardExceptions( | ||
() -> { | ||
brokerShutdownContext.setDiskSpaceUsageMonitor(null); | ||
shutdownFuture.complete(brokerShutdownContext); | ||
}, | ||
shutdownFuture)), | ||
shutdownFuture); | ||
}); | ||
} else { | ||
shutdownFuture.complete(brokerShutdownContext); | ||
} | ||
forwardExceptions( | ||
() -> | ||
concurrencyControl.run( | ||
() -> | ||
forwardExceptions( | ||
() -> { | ||
brokerShutdownContext.setDiskSpaceUsageMonitor(null); | ||
shutdownFuture.complete(brokerShutdownContext); | ||
}, | ||
shutdownFuture)), | ||
shutdownFuture); | ||
}); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "Disk Space Usage Monitor"; | ||
} | ||
|
||
private static final class DisabledDiskUsageMonitor implements DiskSpaceUsageMonitor { | ||
|
||
@Override | ||
public void addDiskUsageListener(final DiskSpaceUsageListener listener) {} | ||
|
||
@Override | ||
public void removeDiskUsageListener(final DiskSpaceUsageListener listener) {} | ||
|
||
@Override | ||
public void setFreeDiskSpaceSupplier(final LongSupplier freeDiskSpaceSupplier) {} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* 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 Zeebe Community License 1.1. You may not use this file | ||
* except in compliance with the Zeebe Community License 1.1. | ||
*/ | ||
package io.camunda.zeebe.broker.system.monitoring; | ||
|
||
import static io.camunda.zeebe.broker.Broker.LOG; | ||
|
||
import io.camunda.zeebe.broker.system.configuration.DataCfg; | ||
import io.camunda.zeebe.scheduler.Actor; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.time.Duration; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.function.LongSupplier; | ||
|
||
public class DiskSpaceUsageMonitorActor extends Actor implements DiskSpaceUsageMonitor { | ||
|
||
private final Set<DiskSpaceUsageListener> diskSpaceUsageListeners = new HashSet<>(); | ||
private boolean currentDiskAvailableStatus = true; | ||
private LongSupplier freeDiskSpaceSupplier; | ||
private final Duration monitoringDelay; | ||
private final long minFreeDiskSpaceRequired; | ||
|
||
public DiskSpaceUsageMonitorActor(final DataCfg dataCfg) { | ||
monitoringDelay = dataCfg.getDiskUsageMonitoringInterval(); | ||
minFreeDiskSpaceRequired = dataCfg.getFreeDiskSpaceCommandWatermark(); | ||
final var directory = new File(dataCfg.getDirectory()); | ||
|
||
if (!directory.exists()) { | ||
throw new UncheckedIOException(new IOException("Folder '" + directory + "' does not exist.")); | ||
} | ||
freeDiskSpaceSupplier = directory::getUsableSpace; | ||
} | ||
|
||
@Override | ||
protected void onActorStarted() { | ||
checkDiskUsageAndNotifyListeners(); | ||
actor.runAtFixedRate(monitoringDelay, this::checkDiskUsageAndNotifyListeners); | ||
} | ||
|
||
private void checkDiskUsageAndNotifyListeners() { | ||
final long freeDiskSpaceAvailable = freeDiskSpaceSupplier.getAsLong(); | ||
final boolean previousStatus = currentDiskAvailableStatus; | ||
currentDiskAvailableStatus = freeDiskSpaceAvailable >= minFreeDiskSpaceRequired; | ||
if (currentDiskAvailableStatus != previousStatus) { | ||
if (!currentDiskAvailableStatus) { | ||
LOG.warn( | ||
"Out of disk space. Current available {} bytes. Minimum needed {} bytes.", | ||
freeDiskSpaceAvailable, | ||
minFreeDiskSpaceRequired); | ||
diskSpaceUsageListeners.forEach(DiskSpaceUsageListener::onDiskSpaceNotAvailable); | ||
} else { | ||
LOG.info("Disk space available again. Current available {} bytes", freeDiskSpaceAvailable); | ||
diskSpaceUsageListeners.forEach(DiskSpaceUsageListener::onDiskSpaceAvailable); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void addDiskUsageListener(final DiskSpaceUsageListener listener) { | ||
actor.call( | ||
() -> { | ||
diskSpaceUsageListeners.add(listener); | ||
// Listeners always assumes diskspace is available on start. So notify them if it is not. | ||
if (!currentDiskAvailableStatus) { | ||
listener.onDiskSpaceNotAvailable(); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void removeDiskUsageListener(final DiskSpaceUsageListener listener) { | ||
actor.call(() -> diskSpaceUsageListeners.remove(listener)); | ||
} | ||
|
||
// Used only for testing | ||
@Override | ||
public void setFreeDiskSpaceSupplier(final LongSupplier freeDiskSpaceSupplier) { | ||
this.freeDiskSpaceSupplier = freeDiskSpaceSupplier; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍