From c139876b4fb2d609dfe6a2501767d4ab95f4261b Mon Sep 17 00:00:00 2001 From: zhangjin Date: Mon, 12 Apr 2021 18:07:25 +0800 Subject: [PATCH] use LongAdder instead of AtomicLong --- .../inf/leaf/segment/SegmentIDGenImpl.java | 10 ++++----- .../inf/leaf/segment/model/Segment.java | 22 ++++++++++++++----- .../controller/LeafMonitorController.java | 4 ++-- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/leaf-core/src/main/java/com/sankuai/inf/leaf/segment/SegmentIDGenImpl.java b/leaf-core/src/main/java/com/sankuai/inf/leaf/segment/SegmentIDGenImpl.java index 21d13518..a2e066af 100644 --- a/leaf-core/src/main/java/com/sankuai/inf/leaf/segment/SegmentIDGenImpl.java +++ b/leaf-core/src/main/java/com/sankuai/inf/leaf/segment/SegmentIDGenImpl.java @@ -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); @@ -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); @@ -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); @@ -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); } @@ -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); } diff --git a/leaf-core/src/main/java/com/sankuai/inf/leaf/segment/model/Segment.java b/leaf-core/src/main/java/com/sankuai/inf/leaf/segment/model/Segment.java index 875f4afb..e750f33d 100644 --- a/leaf-core/src/main/java/com/sankuai/inf/leaf/segment/model/Segment.java +++ b/leaf-core/src/main/java/com/sankuai/inf/leaf/segment/model/Segment.java @@ -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; @@ -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; } @@ -41,7 +41,7 @@ public SegmentBuffer getBuffer() { } public long getIdle() { - return this.getMax() - getValue().get(); + return this.getMax() - getValue().sum(); } @Override @@ -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); + } } diff --git a/leaf-server/src/main/java/com/sankuai/inf/leaf/server/controller/LeafMonitorController.java b/leaf-server/src/main/java/com/sankuai/inf/leaf/server/controller/LeafMonitorController.java index f04d6627..921ca7b5 100644 --- a/leaf-server/src/main/java/com/sankuai/inf/leaf/server/controller/LeafMonitorController.java +++ b/leaf-server/src/main/java/com/sankuai/inf/leaf/server/controller/LeafMonitorController.java @@ -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);