
com.hazelcast.internal.partition.impl.PartitionReplicaVersions Maven / Gradle / Ivy
/*
* Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
*
* 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.hazelcast.internal.partition.impl;
import com.hazelcast.internal.partition.InternalPartition;
import java.util.Arrays;
import static java.lang.System.arraycopy;
// read and updated only by partition threads
final class PartitionReplicaVersions {
private final int partitionId;
private final long[] versions = new long[InternalPartition.MAX_BACKUP_COUNT];
/**
* Shows whether partition has missing backups somewhere between the last applied backup
* and the last incremental backup received.
*/
private boolean dirty;
PartitionReplicaVersions(int partitionId) {
this.partitionId = partitionId;
}
long[] incrementAndGet(int backupCount) {
for (int i = 0; i < backupCount; i++) {
versions[i]++;
}
return versions;
}
long[] get() {
return versions;
}
/**
* Returns whether given replica version is behind the current version or not.
* @param newVersions new replica versions
* @param replicaIndex replica index
* @return true if given version is stale, false otherwise
*/
boolean isStale(long[] newVersions, int replicaIndex) {
int index = replicaIndex - 1;
long currentVersion = versions[index];
long newVersion = newVersions[index];
return currentVersion > newVersion;
}
/**
* Updates replica version if it is newer than current version. Otherwise has no effect.
* Marks versions as dirty if version increase is not incremental.
*
* @param newVersions new replica versions
* @param replicaIndex replica index
* @return returns false if versions are dirty, true otherwise
*/
boolean update(long[] newVersions, int replicaIndex) {
int index = replicaIndex - 1;
long currentVersion = versions[index];
long nextVersion = newVersions[index];
boolean incremental = (currentVersion == nextVersion - 1);
boolean newer = currentVersion < nextVersion;
if (newer) {
setVersions(newVersions, replicaIndex);
dirty = dirty || !incremental;
}
return !dirty;
}
private void setVersions(long[] newVersions, int fromReplica) {
int fromIndex = fromReplica - 1;
int len = newVersions.length - fromIndex;
arraycopy(newVersions, fromIndex, versions, fromIndex, len);
}
void set(long[] newVersions, int fromReplica) {
setVersions(newVersions, fromReplica);
dirty = false;
}
boolean isDirty() {
return dirty;
}
void clear() {
for (int i = 0; i < versions.length; i++) {
versions[i] = 0;
}
dirty = false;
}
@Override
public String toString() {
return getClass().getSimpleName() + "{partitionId=" + partitionId + ", versions=" + Arrays.toString(versions) + '}';
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy