Skip to content

Commit

Permalink
optimize the calculation of safety distance
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang committed Jul 16, 2021
1 parent 8a8d574 commit d0278ff
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ public long getVersion() {
}

public int gap(IdSegmentChain end, long step) {
if (this.equals(end)) {
return 0;
}
return (int) ((end.getOffset() - getOffset()) / step);
return (int) ((end.getMaxId() - getSequence()) / step);
}

public static IdSegmentChain newRoot() {
Expand Down
23 changes: 9 additions & 14 deletions cosid-core/src/main/java/me/ahoo/cosid/segment/SegmentChainId.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public IdSegmentChain getHead() {
private void forward(IdSegmentChain forwardChain) {
if (forwardChain.compareTo(headChain) > 0) {
if (log.isDebugEnabled()) {
log.debug("forward - [{}] -> [{}].", headChain, forwardChain);
log.debug("forward - [{}] - [{}] -> [{}].", maxIdDistributor.getNamespacedName(), headChain, forwardChain);
}
headChain = forwardChain;
}
Expand Down Expand Up @@ -100,12 +100,12 @@ public long generate() {
IdSegmentChain nextChain = preIdSegmentChain.getNext();
forward(nextChain);
if (log.isDebugEnabled()) {
log.debug("generate - headChain.version:[{}->{}].", preIdSegmentChain.getVersion(), nextChain.getVersion());
log.debug("generate - [{}] - headChain.version:[{}->{}].", maxIdDistributor.getNamespacedName(),preIdSegmentChain.getVersion(), nextChain.getVersion());
}
}
} catch (NextIdSegmentExpiredException nextIdSegmentExpiredException) {
if (log.isWarnEnabled()) {
log.warn("generate - gave up this next IdSegmentChain.", nextIdSegmentExpiredException);
log.warn("generate - [{}] - gave up this next IdSegmentChain.", maxIdDistributor.getNamespacedName(),nextIdSegmentExpiredException);
}
}
this.prefetchJob.hungry();
Expand Down Expand Up @@ -171,25 +171,20 @@ public void run() {

public void prefetch() {

if (IdSegmentChain.ROOT_VERSION == tailChain.getVersion()) {
appendChain(tailChain, safeDistance + 1);
return;
}

long wakeupTimeGap = Clock.CACHE.secondTime() - lastHungerTime;
final boolean hunger = wakeupTimeGap < hungerThreshold;

final int prePrefetchDistance = this.prefetchDistance;
if (hunger) {
this.prefetchDistance = Math.min(Math.multiplyExact(this.prefetchDistance, 2), MAX_PREFETCH_DISTANCE);
if (log.isInfoEnabled()) {
log.info("prefetch - {} - Hunger, Safety distance expansion.[{}->{}]", maxIdDistributor.getNamespacedName(), prePrefetchDistance, this.prefetchDistance);
log.info("prefetch - [{}] - Hunger, Safety distance expansion.[{}->{}]", maxIdDistributor.getNamespacedName(), prePrefetchDistance, this.prefetchDistance);
}
} else {
this.prefetchDistance = Math.max(Math.floorDiv(this.prefetchDistance, 2), safeDistance);
if (prePrefetchDistance > this.prefetchDistance) {
if (log.isInfoEnabled()) {
log.info("prefetch - {} - Full, Safety distance shrinks.[{}->{}]", maxIdDistributor.getNamespacedName(), prePrefetchDistance, this.prefetchDistance);
log.info("prefetch - [{}] - Full, Safety distance shrinks.[{}->{}]", maxIdDistributor.getNamespacedName(), prePrefetchDistance, this.prefetchDistance);
}
}
}
Expand All @@ -210,7 +205,7 @@ public void prefetch() {

if (safeGap <= 0 && !hunger) {
if (log.isTraceEnabled()) {
log.trace("prefetch - {} - safeGap is less than or equal to 0, and is not hungry - headChain.version:[{}] - tailChain.version:[{}].", maxIdDistributor.getNamespacedName(), availableHeadChain.getVersion(), tailChain.getVersion());
log.trace("prefetch - [{}] - safeGap is less than or equal to 0, and is not hungry - headChain.version:[{}] - tailChain.version:[{}].", maxIdDistributor.getNamespacedName(), availableHeadChain.getVersion(), tailChain.getVersion());
}
return;
}
Expand All @@ -223,7 +218,7 @@ public void prefetch() {
private void appendChain(IdSegmentChain availableHeadChain, int prefetchSegments) {

if (log.isDebugEnabled()) {
log.debug("appendChain - {} - headChain.version:[{}] - tailChain.version:[{}] - prefetchSegments:[{}].", maxIdDistributor.getNamespacedName(), availableHeadChain.getVersion(), tailChain.getVersion(), prefetchSegments);
log.debug("appendChain - [{}] - headChain.version:[{}] - tailChain.version:[{}] - prefetchSegments:[{}].", maxIdDistributor.getNamespacedName(), availableHeadChain.getVersion(), tailChain.getVersion(), prefetchSegments);
}

try {
Expand All @@ -233,11 +228,11 @@ private void appendChain(IdSegmentChain availableHeadChain, int prefetchSegments
tailChain = tailChain.getNext();
}
if (log.isDebugEnabled()) {
log.debug("appendChain - {} - restTail - tailChain.version:[{}:{}->{}] .", maxIdDistributor.getNamespacedName(), preTail.gap(tailChain, maxIdDistributor.getStep()), preTail.getVersion(), tailChain.getVersion());
log.debug("appendChain - [{}] - restTail - tailChain.version:[{}:{}->{}] .", maxIdDistributor.getNamespacedName(), preTail.gap(tailChain, maxIdDistributor.getStep()), preTail.getVersion(), tailChain.getVersion());
}
} catch (NextIdSegmentExpiredException nextIdSegmentExpiredException) {
if (log.isWarnEnabled()) {
log.warn("appendChain - {} - gave up this next IdSegmentChain.", maxIdDistributor.getNamespacedName(), nextIdSegmentExpiredException);
log.warn("appendChain - [{}] - gave up this next IdSegmentChain.", maxIdDistributor.getNamespacedName(), nextIdSegmentExpiredException);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ public long getStep() {
public long nextMaxId(long step) {
IdSegmentDistributor.ensureStep(step);
long maxId = Futures.getUnChecked(fetchMaxIdAsync(step), timeout);
if (log.isInfoEnabled()) {
log.info("nextMaxId - step:[{}] - maxId:[{}].", step, maxId);
if (log.isDebugEnabled()) {
log.debug("nextMaxId - step:[{}] - maxId:[{}].", step, maxId);
}
return maxId;
}
Expand Down

0 comments on commit d0278ff

Please sign in to comment.