-
Notifications
You must be signed in to change notification settings - Fork 537
HDDS-12554. Support callback on completed reconfiguration #8391
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
Open
sarvekshayr
wants to merge
11
commits into
apache:master
Choose a base branch
from
sarvekshayr:HDDS-12554
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
05735cd
HDDS-12554. Support callback on completed reconfiguration
sarvekshayr c63cde0
Fixed PMD failures
sarvekshayr 1a2a13c
Register the config in BackgroundService
sarvekshayr 9e283a7
Basic CI fixes
sarvekshayr 80fad80
Improved error handling
sarvekshayr ba8e463
Addressed pmd failure
sarvekshayr 7461586
Removed dirDeletingServiceInterval from constructor and its getter fu…
sarvekshayr ad04d20
Use shutdown to interrupt service and validate using getTimeDuration()
sarvekshayr 329f920
Added test to verify interval set
sarvekshayr 7122e55
Merge remote-tracking branch 'origin' into HDDS-12554
sarvekshayr 70fa4e5
Improved the test
sarvekshayr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
212 changes: 212 additions & 0 deletions
212
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/conf/ReconfigurableBase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.hadoop.hdds.conf; | ||
|
||
import com.google.common.collect.Maps; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.Consumer; | ||
import org.apache.hadoop.classification.VisibleForTesting; | ||
import org.apache.hadoop.conf.ConfigRedactor; | ||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.hadoop.conf.Configured; | ||
import org.apache.hadoop.conf.Reconfigurable; | ||
import org.apache.hadoop.conf.ReconfigurationException; | ||
import org.apache.hadoop.conf.ReconfigurationTaskStatus; | ||
import org.apache.hadoop.conf.ReconfigurationUtil; | ||
import org.apache.hadoop.util.Time; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Base class to support dynamic reconfiguration of configuration properties at runtime. | ||
*/ | ||
public abstract class ReconfigurableBase extends Configured implements Reconfigurable { | ||
sarvekshayr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private static final Logger LOG = LoggerFactory.getLogger(ReconfigurableBase.class); | ||
private final ReconfigurationUtil reconfigurationUtil = new ReconfigurationUtil(); | ||
private Thread reconfigThread = null; | ||
private volatile boolean shouldRun = true; | ||
private final Object reconfigLock = new Object(); | ||
private long startTime = 0L; | ||
private long endTime = 0L; | ||
private Map<ReconfigurationUtil.PropertyChange, Optional<String>> status = null; | ||
private final Collection<Consumer<ReconfigurationTaskStatus>> reconfigurationCompleteCallbacks = new ArrayList<>(); | ||
|
||
public ReconfigurableBase(Configuration conf) { | ||
super(conf == null ? new Configuration() : conf); | ||
} | ||
|
||
protected abstract Configuration getNewConf(); | ||
|
||
@VisibleForTesting | ||
public Collection<ReconfigurationUtil.PropertyChange> getChangedProperties(Configuration newConf, | ||
Configuration oldConf) { | ||
return this.reconfigurationUtil.parseChangedProperties(newConf, oldConf); | ||
} | ||
|
||
public void startReconfigurationTask() throws IOException { | ||
synchronized (this.reconfigLock) { | ||
String errorMessage; | ||
if (!this.shouldRun) { | ||
errorMessage = "The server is stopped."; | ||
LOG.warn(errorMessage); | ||
throw new IOException(errorMessage); | ||
} else if (this.reconfigThread != null) { | ||
errorMessage = "Another reconfiguration task is running."; | ||
LOG.warn(errorMessage); | ||
throw new IOException(errorMessage); | ||
} else { | ||
this.reconfigThread = new ReconfigurationThread(this); | ||
this.reconfigThread.setDaemon(true); | ||
this.reconfigThread.setName("Reconfiguration Task"); | ||
this.reconfigThread.start(); | ||
this.startTime = Time.now(); | ||
} | ||
} | ||
} | ||
|
||
public ReconfigurationTaskStatus getReconfigurationTaskStatus() { | ||
synchronized (this.reconfigLock) { | ||
return this.reconfigThread != null ? new ReconfigurationTaskStatus(this.startTime, 0L, null) : | ||
new ReconfigurationTaskStatus(this.startTime, this.endTime, this.status); | ||
} | ||
} | ||
|
||
public void shutdownReconfigurationTask() { | ||
Thread tempThread; | ||
synchronized (this.reconfigLock) { | ||
this.shouldRun = false; | ||
if (this.reconfigThread == null) { | ||
return; | ||
} | ||
|
||
tempThread = this.reconfigThread; | ||
this.reconfigThread = null; | ||
} | ||
|
||
try { | ||
tempThread.join(); | ||
} catch (InterruptedException ignored) { | ||
sarvekshayr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
} | ||
|
||
@Override | ||
public final void reconfigureProperty(String property, String newVal) throws ReconfigurationException { | ||
if (this.isPropertyReconfigurable(property)) { | ||
LOG.info("changing property " + property + " to " + newVal); | ||
synchronized (this.getConf()) { | ||
this.getConf().get(property); | ||
String effectiveValue = this.reconfigurePropertyImpl(property, newVal); | ||
if (newVal != null) { | ||
this.getConf().set(property, effectiveValue); | ||
} else { | ||
this.getConf().unset(property); | ||
} | ||
|
||
sarvekshayr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} else { | ||
throw new ReconfigurationException(property, newVal, this.getConf().get(property)); | ||
} | ||
} | ||
|
||
@Override | ||
public abstract Collection<String> getReconfigurableProperties(); | ||
|
||
@Override | ||
public boolean isPropertyReconfigurable(String property) { | ||
return this.getReconfigurableProperties().contains(property); | ||
} | ||
|
||
protected abstract String reconfigurePropertyImpl(String var1, String var2) throws ReconfigurationException; | ||
|
||
private static class ReconfigurationThread extends Thread { | ||
private final ReconfigurableBase parent; | ||
|
||
ReconfigurationThread(ReconfigurableBase base) { | ||
this.parent = base; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
LOG.info("Starting reconfiguration task."); | ||
Configuration oldConf = this.parent.getConf(); | ||
Configuration newConf = this.parent.getNewConf(); | ||
Collection<ReconfigurationUtil.PropertyChange> changes = this.parent.getChangedProperties(newConf, oldConf); | ||
Map<ReconfigurationUtil.PropertyChange, Optional<String>> results = Maps.newHashMap(); | ||
ConfigRedactor oldRedactor = new ConfigRedactor(oldConf); | ||
ConfigRedactor newRedactor = new ConfigRedactor(newConf); | ||
|
||
for (ReconfigurationUtil.PropertyChange change : changes) { | ||
String errorMessage = null; | ||
String oldValRedacted = oldRedactor.redact(change.prop, change.oldVal); | ||
String newValRedacted = newRedactor.redact(change.prop, change.newVal); | ||
if (!this.parent.isPropertyReconfigurable(change.prop)) { | ||
LOG.info(String.format("Property %s is not configurable: old value: %s, new value: %s", | ||
change.prop, oldValRedacted, newValRedacted)); | ||
} else { | ||
LOG.info("Change property: " + change.prop + " from \"" + | ||
(change.oldVal == null ? "<default>" : oldValRedacted) + "\" to \"" + | ||
(change.newVal == null ? "<default>" : newValRedacted) + "\"."); | ||
|
||
try { | ||
String effectiveValue = this.parent.reconfigurePropertyImpl(change.prop, change.newVal); | ||
if (change.newVal != null) { | ||
oldConf.set(change.prop, effectiveValue); | ||
} else { | ||
oldConf.unset(change.prop); | ||
} | ||
} catch (ReconfigurationException reconfException) { | ||
Throwable cause = reconfException.getCause(); | ||
errorMessage = cause == null ? reconfException.getMessage() : cause.getMessage(); | ||
LOG.error("Failed to reconfigure property {}: {}", change.prop, errorMessage, reconfException); | ||
} | ||
|
||
results.put(change, Optional.ofNullable(errorMessage)); | ||
} | ||
} | ||
|
||
synchronized (this.parent.reconfigLock) { | ||
this.parent.endTime = Time.now(); | ||
this.parent.status = Collections.unmodifiableMap(results); | ||
this.parent.reconfigThread = null; | ||
|
||
LOG.info("Reconfiguration completed. {} properties were updated.", results.size()); | ||
|
||
for (Consumer<ReconfigurationTaskStatus> callback : parent.reconfigurationCompleteCallbacks) { | ||
try { | ||
callback.accept(parent.getReconfigurationTaskStatus()); | ||
} catch (Exception e) { | ||
LOG.warn("Reconfiguration complete callback threw exception", e); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void addReconfigurationCompleteCallback(Consumer<ReconfigurationTaskStatus> callback) { | ||
synchronized (reconfigLock) { | ||
this.reconfigurationCompleteCallbacks.add(callback); | ||
} | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...ds/framework/src/main/java/org/apache/hadoop/hdds/conf/ReconfigurationChangeCallback.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.hadoop.hdds.conf; | ||
|
||
import java.util.Map; | ||
import org.apache.hadoop.conf.Configuration; | ||
|
||
/** | ||
* Callback interface to handle configuration changes after a reconfiguration task completes. | ||
*/ | ||
@FunctionalInterface | ||
public interface ReconfigurationChangeCallback { | ||
void onPropertiesChanged(Map<String, Boolean> changedKeys, Configuration newConf); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.