KLALB 3.6.0开发一半的状态
This commit is contained in:
@@ -3,8 +3,6 @@ package org.kne.cloud.network.ntp;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.security.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
@@ -16,13 +14,8 @@ import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.Vector;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import org.fusesource.jansi.io.AnsiOutputStream.ZeroWidthSupplier;
|
||||
import org.kne.cloud.clock.HighAccuracyClock;
|
||||
import org.kne.cloud.clock.NTPTimestamps;
|
||||
import org.kne.cloud.network.MultipurposeSocketAddress;
|
||||
@@ -33,12 +26,12 @@ import org.kne.math.Long128;
|
||||
public class NTPContext implements Closeable, AutoCloseable {
|
||||
private HighAccuracyClock clock;
|
||||
private static final boolean debug = false;
|
||||
private static final int REQUEST_COUNT = 10;
|
||||
private static final int REQUEST_COUNT = 5;
|
||||
|
||||
private Long128 systemFrequencyOffset = NTPTimestamps.nanosToNtp128BitTimeInterval(new Long128(5000));
|
||||
private Long128 localPrecision = NTPTimestamps.nanosToNtp128BitTimeInterval(new Long128(1000));
|
||||
private static final Long128 adjustThreshold0 = Long128.valueOf(1000000000L);
|
||||
private static final Long128 adjustThreshold1 = Long128.valueOf(2000000L);
|
||||
private static final Long128 adjustThreshold1 = Long128.valueOf(100000000L);
|
||||
private AtomicBoolean firstSync=new AtomicBoolean(true);
|
||||
|
||||
private int minStratum = 16;
|
||||
@@ -74,7 +67,7 @@ public class NTPContext implements Closeable, AutoCloseable {
|
||||
|
||||
Thread.sleep(100);
|
||||
}
|
||||
Thread.sleep(1000);
|
||||
Thread.sleep(500);
|
||||
|
||||
if (!mlp.isEmpty())
|
||||
mergeAndApply();
|
||||
@@ -222,9 +215,8 @@ public class NTPContext implements Closeable, AutoCloseable {
|
||||
selectAndApply(peerInfo);
|
||||
}
|
||||
|
||||
private AtomicLong prevAdjustTime = new AtomicLong(System.nanoTime());
|
||||
private volatile Long128 deltaRemaining;
|
||||
private long avgAdj=0;
|
||||
private Long128 inte=new Long128(0);
|
||||
private void selectAndApply(List<PeerInfo> peerInfo) {
|
||||
Collections.sort(peerInfo);
|
||||
if(debug)
|
||||
@@ -240,31 +232,34 @@ public class NTPContext implements Closeable, AutoCloseable {
|
||||
Long128 adjt = pi.getAdj();
|
||||
bi = bi.add(adjt);
|
||||
}
|
||||
Long128 adj = bi.divide(Long128.valueOf(i));
|
||||
avgAdj=(avgAdj*7+adj.abs().longValue())/8;
|
||||
Long128 adjustment;
|
||||
Long128 adjabs=adj.abs();
|
||||
if (adjabs.compareTo(adjustThreshold0) > 0&&firstSync.compareAndSet(true, false)) {
|
||||
adjustment=adj;
|
||||
}else if (adjabs.compareTo(adjustThreshold1) > 0) {
|
||||
adjustment = Long128.valueOf(new BigDecimal(adj.toBigInteger()).multiply(BigDecimal.valueOf(0.5)).toBigInteger());
|
||||
Long128 delta = bi.divide(Long128.valueOf(i));
|
||||
avgAdj=(avgAdj*7+delta.abs().longValue())/8;
|
||||
Long128 deltaabs=delta.abs();
|
||||
if (deltaabs.compareTo(adjustThreshold0) > 0&&firstSync.compareAndSet(true, false)) {
|
||||
Long128 adjustment=delta;
|
||||
clock.adjustClock( adjustment);
|
||||
if(debug)
|
||||
System.out.println("adj:"+delta);
|
||||
}else if (deltaabs.compareTo(adjustThreshold1) > 0) {
|
||||
Long128 adjustment = Long128.valueOf(new BigDecimal(delta.toBigInteger()).multiply(BigDecimal.valueOf(0.5)).toBigInteger());
|
||||
clock.adjustClock( adjustment);
|
||||
if(debug)
|
||||
System.out.println("adj:"+delta);
|
||||
} else {
|
||||
adjustment = Long128.valueOf(new BigDecimal(adj.toBigInteger()).multiply(BigDecimal.valueOf(0.0625/2)).toBigInteger());
|
||||
if (deltaRemaining != null) {
|
||||
long curr = System.nanoTime();
|
||||
long old = prevAdjustTime.getAndSet(curr);
|
||||
Long128 fadj = adjustment.multiply(Long128.valueOf(TimeUnit.SECONDS.toNanos(1)))
|
||||
.divide(Long128.valueOf(curr - old));
|
||||
Long128 fadjustment = Long128.valueOf( new BigDecimal(fadj.toBigInteger()).multiply(BigDecimal.valueOf(0.015625/4)).toBigInteger());
|
||||
clock.adjustFrequency(fadjustment.longValue());
|
||||
Long128 fadj = delta.divide(20).add(inte.divide(200));
|
||||
clock.setFrequency(1000000000L+fadj.longValue());
|
||||
inte=inte.add(delta);
|
||||
if(inte.compareTo(Long128.valueOf(5000000))>0) {
|
||||
inte=Long128.valueOf(5000000);
|
||||
}else if(inte.compareTo(Long128.valueOf(-5000000))<0) {
|
||||
inte=Long128.valueOf(-5000000);
|
||||
}
|
||||
if(debug)
|
||||
System.out.println("fadj:" + fadjustment + " f:" + clock.getFrequency());
|
||||
}
|
||||
deltaRemaining = adj.subtract(adjustment);
|
||||
System.out.println("inte:"+inte+" fadj:" + fadj);
|
||||
|
||||
}
|
||||
clock.adjustClock( adjustment);
|
||||
if(debug)
|
||||
System.out.println("delta:" + adjustment + " deltaRemaining:" + deltaRemaining);
|
||||
System.out.println("delta:"+delta);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user