-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
892 additions
and
483 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
216 changes: 216 additions & 0 deletions
216
core/src/main/java/com/ericsson/bss/cassandra/ecchronos/core/CASLock.java
This file contains 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,216 @@ | ||
/* | ||
* Copyright 2024 Telefonaktiebolaget LM Ericsson | ||
* | ||
* 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. | ||
*/ | ||
package com.ericsson.bss.cassandra.ecchronos.core; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.ScheduledFuture; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.datastax.oss.driver.api.core.cql.ResultSet; | ||
import com.datastax.oss.driver.api.core.cql.Row; | ||
import com.ericsson.bss.cassandra.ecchronos.core.exceptions.LockException; | ||
import com.ericsson.bss.cassandra.ecchronos.core.scheduling.LockFactory.DistributedLock; | ||
|
||
/** | ||
* Represents a container for builder configurations and state for the CASLock. | ||
* This class is used to decouple builder fields from CASLockFactory to avoid excessive field count. | ||
*/ | ||
class CASLock implements DistributedLock, Runnable | ||
{ | ||
private static final Logger LOG = LoggerFactory.getLogger(CASLock.class); | ||
|
||
private final String myDataCenter; | ||
private final String myResource; | ||
private final int myPriority; | ||
private final Map<String, String> myMetadata; | ||
|
||
private final AtomicReference<ScheduledFuture<?>> myUpdateFuture = new AtomicReference<>(); | ||
|
||
private final AtomicInteger myFailedUpdateAttempts = new AtomicInteger(); | ||
|
||
private final int myLocallyHighestPriority; | ||
private final int globalHighPriority; | ||
|
||
private final UUID myUuid; | ||
|
||
private final CASLockStatement myCasLockStatement; | ||
|
||
CASLock(final String dataCenter, | ||
final String resource, | ||
final int priority, | ||
final Map<String, String> metadata, | ||
final UUID uuid, | ||
final CASLockStatement casLockStatement) | ||
{ | ||
myDataCenter = dataCenter; | ||
myResource = resource; | ||
myPriority = priority; | ||
myMetadata = metadata; | ||
myUuid = uuid; | ||
myCasLockStatement = casLockStatement; | ||
|
||
List<NodePriority> nodePriorities = computePriorities(); | ||
|
||
myLocallyHighestPriority = nodePriorities.stream() | ||
.filter(n -> n.getUuid().equals(myUuid)) | ||
.map(NodePriority::getPriority) | ||
.findFirst() | ||
.orElse(myPriority); | ||
globalHighPriority = nodePriorities.stream() | ||
.filter(n -> !n.getUuid().equals(myUuid)) | ||
.map(NodePriority::getPriority) | ||
.max(Integer::compare) | ||
.orElse(myPriority); | ||
} | ||
|
||
public boolean lock() | ||
{ | ||
if (compete()) | ||
{ | ||
LOG.trace("Trying to acquire lock for resource {}", myResource); | ||
if (tryLock()) | ||
{ | ||
ScheduledExecutorService executor = myCasLockStatement.getCasLockProperties().getExecutor(); | ||
LOG.trace("Lock for resource {} acquired", myResource); | ||
ScheduledFuture<?> future = executor.scheduleAtFixedRate(this, | ||
myCasLockStatement.getCasLockFactoryCacheContext().getLockUpdateTimeInSeconds(), | ||
myCasLockStatement.getCasLockFactoryCacheContext().getLockUpdateTimeInSeconds(), TimeUnit.SECONDS); | ||
myUpdateFuture.set(future); | ||
|
||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
@Override | ||
public void run() | ||
{ | ||
try | ||
{ | ||
updateLock(); | ||
myFailedUpdateAttempts.set(0); | ||
} | ||
catch (LockException e) | ||
{ | ||
int failedAttempts = myFailedUpdateAttempts.incrementAndGet(); | ||
|
||
if (failedAttempts >= myCasLockStatement.getCasLockFactoryCacheContext().getFailedLockRetryAttempts()) | ||
{ | ||
LOG.error("Unable to re-lock resource '{}' after {} failed attempts", myResource, failedAttempts); | ||
} | ||
else | ||
{ | ||
LOG.warn("Unable to re-lock resource '{}', {} failed attempts", myResource, failedAttempts, e); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void close() | ||
{ | ||
ScheduledFuture<?> future = myUpdateFuture.get(); | ||
if (future != null) | ||
{ | ||
future.cancel(true); | ||
myCasLockStatement.execute( | ||
myDataCenter, | ||
myCasLockStatement.getRemoveLockStatement().bind(myResource, myUuid)); | ||
|
||
if (myLocallyHighestPriority <= myPriority) | ||
{ | ||
myCasLockStatement.execute( | ||
myDataCenter, | ||
myCasLockStatement.getRemoveLockPriorityStatement().bind(myResource, myUuid)); | ||
} | ||
else | ||
{ | ||
LOG.debug("Locally highest priority ({}) is higher than current ({}), will not remove", | ||
myLocallyHighestPriority, myPriority); | ||
} | ||
} | ||
} | ||
|
||
private void updateLock() throws LockException | ||
{ | ||
ResultSet resultSet = myCasLockStatement.execute(myDataCenter, | ||
myCasLockStatement.getUpdateLockStatement().bind(myUuid, myMetadata, myResource, myUuid)); | ||
|
||
if (!resultSet.wasApplied()) | ||
{ | ||
throw new LockException("CAS query failed"); | ||
} | ||
} | ||
|
||
private boolean compete() | ||
{ | ||
if (myLocallyHighestPriority <= myPriority) | ||
{ | ||
insertPriority(); | ||
} | ||
|
||
LOG.trace("Highest priority for resource {}: {}", myResource, globalHighPriority); | ||
return myPriority >= globalHighPriority; | ||
} | ||
|
||
private void insertPriority() | ||
{ | ||
myCasLockStatement.execute( | ||
myDataCenter, | ||
myCasLockStatement.getCompeteStatement().bind(myResource, myUuid, myPriority)); | ||
} | ||
|
||
private boolean tryLock() | ||
{ | ||
return myCasLockStatement.execute( | ||
myDataCenter, | ||
myCasLockStatement.getLockStatement().bind(myResource, myUuid, myMetadata)).wasApplied(); | ||
} | ||
|
||
private List<NodePriority> computePriorities() | ||
{ | ||
List<NodePriority> nodePriorities = new ArrayList<>(); | ||
|
||
ResultSet resultSet = myCasLockStatement.execute( | ||
myDataCenter, | ||
myCasLockStatement.getGetPriorityStatement().bind(myResource)); | ||
|
||
for (Row row : resultSet) | ||
{ | ||
int priority = row.getInt(CASLockStatement.COLUMN_PRIORITY); | ||
UUID hostId = row.getUuid(CASLockStatement.COLUMN_NODE); | ||
|
||
nodePriorities.add(new NodePriority(hostId, priority)); | ||
} | ||
|
||
return nodePriorities; | ||
} | ||
|
||
int getFailedAttempts() | ||
{ | ||
return myFailedUpdateAttempts.get(); | ||
} | ||
|
||
} |
Oops, something went wrong.