Skip to content
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

use LongAdder instead of AtomicLong #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;

public class SegmentIDGenImpl implements IDGen {
private static final Logger logger = LoggerFactory.getLogger(SegmentIDGenImpl.class);
Expand Down Expand Up @@ -106,7 +106,7 @@ private void updateCacheFromDb() {
SegmentBuffer buffer = new SegmentBuffer();
buffer.setKey(tag);
Segment segment = buffer.getCurrent();
segment.setValue(new AtomicLong(0));
segment.setValue(new LongAdder());
segment.setMax(0);
segment.setStep(0);
cache.put(tag, buffer);
Expand Down Expand Up @@ -193,7 +193,7 @@ public void updateSegmentFromDb(String key, Segment segment) {
}
// must set value before set max
long value = leafAlloc.getMaxId() - buffer.getStep();
segment.getValue().set(value);
segment.reset(value);
segment.setMax(leafAlloc.getMaxId());
segment.setStep(buffer.getStep());
sw.stop("updateSegmentFromDb", key + " " + segment);
Expand Down Expand Up @@ -229,7 +229,7 @@ public void run() {
}
});
}
long value = segment.getValue().getAndIncrement();
long value = segment.getAndIncrement();
if (value < segment.getMax()) {
return new Result(value, Status.SUCCESS);
}
Expand All @@ -240,7 +240,7 @@ public void run() {
buffer.wLock().lock();
try {
final Segment segment = buffer.getCurrent();
long value = segment.getValue().getAndIncrement();
long value = segment.getAndIncrement();
if (value < segment.getMax()) {
return new Result(value, Status.SUCCESS);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.sankuai.inf.leaf.segment.model;

import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;

public class Segment {
private AtomicLong value = new AtomicLong(0);
private LongAdder value = new LongAdder();
private volatile long max;
private volatile int step;
private SegmentBuffer buffer;
Expand All @@ -12,11 +12,11 @@ public Segment(SegmentBuffer buffer) {
this.buffer = buffer;
}

public AtomicLong getValue() {
public LongAdder getValue() {
return value;
}

public void setValue(AtomicLong value) {
public void setValue(LongAdder value) {
this.value = value;
}

Expand All @@ -41,7 +41,7 @@ public SegmentBuffer getBuffer() {
}

public long getIdle() {
return this.getMax() - getValue().get();
return this.getMax() - getValue().sum();
}

@Override
Expand All @@ -56,4 +56,16 @@ public String toString() {
sb.append(")");
return sb.toString();
}

// get then increment
public long getAndIncrement(){
long ret = value.sum();
value.increment();
return ret;
}

public void reset(long value) {
this.value.reset();
this.value.add(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public String getCache(Model model) {
sv.setPos(buffer.getCurrentPos());
sv.setNextReady(buffer.isNextReady());
sv.setMax0(buffer.getSegments()[0].getMax());
sv.setValue0(buffer.getSegments()[0].getValue().get());
sv.setValue0(buffer.getSegments()[0].getValue().sum());
sv.setStep0(buffer.getSegments()[0].getStep());

sv.setMax1(buffer.getSegments()[1].getMax());
sv.setValue1(buffer.getSegments()[1].getValue().get());
sv.setValue1(buffer.getSegments()[1].getValue().sum());
sv.setStep1(buffer.getSegments()[1].getStep());

data.put(entry.getKey(), sv);
Expand Down