Skip to content

Commit

Permalink
#455 #456 fix wrong behavior of all optimizations when implicit confi…
Browse files Browse the repository at this point in the history
…guration replacement had been configured and two parallel threads in same time detected that version should be changed
  • Loading branch information
Vladimir Buhtoyarov committed Mar 16, 2024
1 parent 09eb42e commit 0e02b9e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 3 deletions.
@@ -0,0 +1,30 @@
/*-
* ========================LICENSE_START=================================
* Bucket4j
* %%
* Copyright (C) 2015 - 2020 Vladimir Bukhtoyarov
* %%
* Licensed 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.
* =========================LICENSE_END==================================
*/

package io.github.bucket4j.distributed.proxy;


public class ConfigurationNeedToBeReplacedException extends IllegalStateException {

public ConfigurationNeedToBeReplacedException() {
super("Bucket configuration need to be replaced");
}

}
Expand Up @@ -113,6 +113,9 @@ private <T> CommandResult<T> tryConsumeLocally(RemoteCommand<T> command) {
// execute local command
MutableBucketEntry entry = new MutableBucketEntry(state.copy());
CommandResult<T> result = command.execute(entry, currentTimeNanos);
if (result.isConfigurationNeedToBeReplaced()) {
return null;
}
long locallyConsumedTokens = command.getConsumedTokens(result.getData());
if (locallyConsumedTokens == Long.MAX_VALUE) {
return null;
Expand Down
Expand Up @@ -138,6 +138,9 @@ private <T> CommandResult<T> tryConsumeLocally(RemoteCommand<T> command) {
// execute local command
MutableBucketEntry entry = new MutableBucketEntry(state.copy());
CommandResult<T> result = command.execute(entry, currentTimeNanos);
if (result.isConfigurationNeedToBeReplaced()) {
return null;
}
long locallyConsumedTokens = command.getConsumedTokens(result.getData());
if (locallyConsumedTokens == Long.MAX_VALUE) {
return null;
Expand Down
Expand Up @@ -129,6 +129,9 @@ private <T> CommandResult<T> tryConsumeLocally(RemoteCommand<T> command) {
// execute local command
MutableBucketEntry entry = new MutableBucketEntry(state.copy());
MultiResult multiResult = multiCommand.execute(entry, currentTimeNanos).getData();
if (multiResult.getResults().get(1).isConfigurationNeedToBeReplaced()) {
return null;
}

if (multiCommand.getConsumedTokens(multiResult) == Long.MAX_VALUE) {
return null;
Expand Down
Expand Up @@ -110,6 +110,9 @@ private <T> CommandResult<T> tryExecuteLocally(RemoteCommand<T> command) {
// execute local command
MutableBucketEntry entry = new MutableBucketEntry(state.copy());
CommandResult<T> result = command.execute(entry, currentTimeNanos);
if (result.isConfigurationNeedToBeReplaced()) {
return null;
}
long locallyConsumedTokens = command.getConsumedTokens(result.getData());
if (locallyConsumedTokens == Long.MAX_VALUE) {
return null;
Expand Down
Expand Up @@ -19,7 +19,7 @@
*/
package io.github.bucket4j.distributed.remote;

import io.github.bucket4j.distributed.proxy.BucketNotFoundException;
import io.github.bucket4j.distributed.proxy.ConfigurationNeedToBeReplacedException;
import io.github.bucket4j.distributed.serialization.DeserializationAdapter;
import io.github.bucket4j.distributed.serialization.Scope;
import io.github.bucket4j.distributed.serialization.SerializationAdapter;
Expand All @@ -40,7 +40,7 @@ public class ConfigurationNeedToBeReplacedError implements CommandError, Compara

@Override
public RuntimeException asException() {
return new BucketNotFoundException();
return new ConfigurationNeedToBeReplacedException();
}

public static SerializationHandle<ConfigurationNeedToBeReplacedError> SERIALIZATION_HANDLE = new SerializationHandle<>() {
Expand All @@ -63,7 +63,7 @@ public int getTypeId() {

@Override
public Class<ConfigurationNeedToBeReplacedError> getSerializedType() {
return (Class) ConfigurationNeedToBeReplacedError.class;
return ConfigurationNeedToBeReplacedError.class;
}

@Override
Expand Down

0 comments on commit 0e02b9e

Please sign in to comment.