Skip to content

Commit 4048d8e

Browse files
author
Josh Gordineer
committed
remove netflix commons
1 parent a634f1d commit 4048d8e

File tree

13 files changed

+1269
-3
lines changed

13 files changed

+1269
-3
lines changed

ribbon-httpclient/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ dependencies {
99
api "org.slf4j:slf4j-api:${slf4j_version}"
1010
api "com.netflix.servo:servo-core:${servo_version}"
1111
api "com.google.guava:guava:${guava_version}"
12-
api 'com.netflix.netflix-commons:netflix-commons-util:0.1.1'
1312
testImplementation 'junit:junit:4.11'
1413
testImplementation "org.slf4j:slf4j-log4j12:${slf4j_version}"
1514
testImplementation 'commons-io:commons-io:2.0.1'

ribbon-loadbalancer/build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
dependencies {
22
api project(':ribbon-core')
3-
api 'com.netflix.netflix-commons:netflix-statistics:0.1.1'
43
api "io.reactivex:rxjava:${rx_java_version}"
54
api "org.slf4j:slf4j-api:${slf4j_version}"
65
api "com.netflix.servo:servo-core:${servo_version}"
76
api "com.google.guava:guava:${guava_version}"
8-
api 'com.netflix.netflix-commons:netflix-commons-util:0.1.1'
97

108
testImplementation project(":ribbon-archaius")
119
testImplementation 'junit:junit:4.11'
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
*
3+
* Copyright 2013 Netflix, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
package com.netflix.stats.distribution;
19+
20+
import java.util.concurrent.locks.Lock;
21+
22+
23+
/**
24+
* A double-buffer of {@link DataBuffer} objects.
25+
* One is the "current" buffer, and new data is added to it.
26+
* The other is the "previous" buffer, and is used as a sorce
27+
* of computed statistics.
28+
*
29+
* @see DataPublisher
30+
*
31+
* @author $Author: netflixoss $
32+
*/
33+
public abstract class DataAccumulator implements DataCollector {
34+
35+
private DataBuffer current;
36+
private DataBuffer previous;
37+
private final Object swapLock = new Object();
38+
39+
/*
40+
* Constructor(s)
41+
*/
42+
43+
/**
44+
* Creates a new initially empty DataAccumulator.
45+
*
46+
* @param bufferSize the size of the buffers to use
47+
*/
48+
public DataAccumulator(int bufferSize) {
49+
this.current = new DataBuffer(bufferSize);
50+
this.previous = new DataBuffer(bufferSize);
51+
}
52+
53+
/*
54+
* Accumulating new values
55+
*/
56+
57+
/** {@inheritDoc} */
58+
public void noteValue(double val) {
59+
synchronized (swapLock) {
60+
Lock l = current.getLock();
61+
l.lock();
62+
try {
63+
current.noteValue(val);
64+
} finally {
65+
l.unlock();
66+
}
67+
}
68+
}
69+
70+
/**
71+
* Swaps the data collection buffers, and computes statistics
72+
* about the data collected up til now.
73+
*/
74+
public void publish() {
75+
/*
76+
* Some care is required here to correctly swap the DataBuffers,
77+
* but not hold the synchronization object while compiling stats
78+
* (a potentially long computation). This ensures that continued
79+
* data collection (calls to noteValue()) will not be blocked for any
80+
* significant period.
81+
*/
82+
DataBuffer tmp = null;
83+
Lock l = null;
84+
synchronized (swapLock) {
85+
// Swap buffers
86+
tmp = current;
87+
current = previous;
88+
previous = tmp;
89+
// Start collection in the new "current" buffer
90+
l = current.getLock();
91+
l.lock();
92+
try {
93+
current.startCollection();
94+
} finally {
95+
l.unlock();
96+
}
97+
// Grab lock on new "previous" buffer
98+
l = tmp.getLock();
99+
l.lock();
100+
}
101+
// Release synchronizaton *before* publishing data
102+
try {
103+
tmp.endCollection();
104+
publish(tmp);
105+
} finally {
106+
l.unlock();
107+
}
108+
}
109+
110+
/**
111+
* Called to publish recently collected data.
112+
* When called, the {@link Lock} associated with the "previous"
113+
* buffer is held, so the data will not be changed.
114+
* Other locks have been released, and so new data can be
115+
* collected in the "current" buffer.
116+
* The data in the buffer has also been sorted in increasing order.
117+
*
118+
* @param buf the {@code DataBuffer} that is now "previous".
119+
*/
120+
protected abstract void publish(DataBuffer buf);
121+
122+
} // DataAccumulator
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
/*
2+
*
3+
* Copyright 2013 Netflix, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
package com.netflix.stats.distribution;
19+
20+
import java.util.Arrays;
21+
import java.util.concurrent.locks.Lock;
22+
import java.util.concurrent.locks.ReentrantLock;
23+
24+
25+
/**
26+
* A fixed-size data collection buffer that holds a sliding window
27+
* of the most recent values added.
28+
* The {@code DataBuffer} is also a {@link Distribution} and so collects
29+
* basic statistics about the data added to the buffer.
30+
* This statistical data is managed on-the-fly, and reflects all the data
31+
* added, if those values that may have been dropped due to buffer overflow.
32+
* <p>
33+
* This class is <em>not</em> synchronized, but can instead managed by a
34+
* {@link Lock} attached to the {@code DataBuffer} (see {@link #getLock}).
35+
* @author netflixoss
36+
*/
37+
public class DataBuffer extends Distribution {
38+
39+
private final Lock lock;
40+
private final double[] buf;
41+
private long startMillis;
42+
private long endMillis;
43+
private int size;
44+
private int insertPos;
45+
46+
/*
47+
* Constructors
48+
*/
49+
50+
/**
51+
* Creates a new {@code DataBuffer} with a given capacity.
52+
*/
53+
public DataBuffer(int capacity) {
54+
lock = new ReentrantLock();
55+
buf = new double[capacity];
56+
startMillis = 0;
57+
size = 0;
58+
insertPos = 0;
59+
}
60+
61+
/*
62+
* Accessors
63+
*/
64+
65+
/**
66+
* Gets the {@link Lock} to use to manage access to the
67+
* contents of the {@code DataBuffer}.
68+
*/
69+
public Lock getLock() {
70+
return lock;
71+
}
72+
73+
/**
74+
* Gets the capacity of the {@code DataBuffer}; that is,
75+
* the maximum number of values that the {@code DataBuffer} can hold.
76+
*/
77+
public int getCapacity() {
78+
return buf.length;
79+
}
80+
81+
/**
82+
* Gets the length of time over which the data was collected,
83+
* in milliseconds.
84+
* The value is only valid after {@link #endCollection}
85+
* has been called (and before a subsequent call to {@link #startCollection}).
86+
*/
87+
public long getSampleIntervalMillis() {
88+
return (endMillis - startMillis);
89+
}
90+
91+
/**
92+
* Gets the number of values currently held in the buffer.
93+
* This value may be smaller than the value of {@link #getNumValues}
94+
* depending on how the percentile values were computed.
95+
*/
96+
public int getSampleSize() {
97+
return size;
98+
}
99+
100+
/*
101+
* Managing the data
102+
*/
103+
104+
/** {@inheritDoc} */
105+
@Override
106+
public void clear() {
107+
super.clear();
108+
startMillis = 0;
109+
size = 0;
110+
insertPos = 0;
111+
}
112+
113+
/**
114+
* Notifies the buffer that data is collection is now enabled.
115+
*/
116+
public void startCollection() {
117+
clear();
118+
startMillis = System.currentTimeMillis();
119+
}
120+
121+
/**
122+
* Notifies the buffer that data has just ended.
123+
* <p>
124+
* <b>Performance Note:</b>
125+
* <br>This method sorts the underlying data buffer,
126+
* and so may be slow. It is best to call this at most once
127+
* and fetch all percentile values desired, instead of making
128+
* a number of repeated calls.
129+
*/
130+
public void endCollection() {
131+
endMillis = System.currentTimeMillis();
132+
Arrays.sort(buf, 0, size);
133+
}
134+
135+
/**
136+
* {@inheritDoc}
137+
* <p>
138+
* The buffer wraps-around if it is full, overwriting the oldest
139+
* entry with the new value.
140+
*/
141+
@Override
142+
public void noteValue(double val) {
143+
super.noteValue(val);
144+
buf[insertPos++] = val;
145+
if (insertPos >= buf.length) {
146+
insertPos = 0;
147+
size = buf.length;
148+
} else if (insertPos > size) {
149+
size = insertPos;
150+
}
151+
}
152+
153+
/**
154+
* Gets the requested percentile statistics.
155+
*
156+
* @param percents array of percentile values to compute,
157+
* which must be in the range {@code [0 .. 100]}
158+
* @param percentiles array to fill in with the percentile values;
159+
* must be the same length as {@code percents}
160+
* @return the {@code percentiles} array
161+
* @see <a href="http://en.wikipedia.org/wiki/Percentile">Percentile (Wikipedia)</a>
162+
* @see <a href="http://cnx.org/content/m10805/latest/">Percentile</a>
163+
*/
164+
public double[] getPercentiles(double[] percents, double[] percentiles) {
165+
for (int i = 0; i < percents.length; i++) {
166+
percentiles[i] = computePercentile(percents[i]);
167+
}
168+
return percentiles;
169+
}
170+
171+
private double computePercentile(double percent) {
172+
// Some just-in-case edge cases
173+
if (size <= 0) {
174+
return 0.0;
175+
} else if (percent <= 0.0) {
176+
return buf[0];
177+
} else if (percent >= 100.0) { // SUPPRESS CHECKSTYLE MagicNumber
178+
return buf[size - 1];
179+
}
180+
/*
181+
* Note:
182+
* Documents like http://cnx.org/content/m10805/latest
183+
* use a one-based ranking, while this code uses a zero-based index,
184+
* so the code may not look exactly like the formulas.
185+
*/
186+
double index = (percent / 100.0) * size; // SUPPRESS CHECKSTYLE MagicNumber
187+
int iLow = (int) Math.floor(index);
188+
int iHigh = (int) Math.ceil(index);
189+
assert 0 <= iLow && iLow <= index && index <= iHigh && iHigh <= size;
190+
assert (iHigh - iLow) <= 1;
191+
if (iHigh >= size) {
192+
// Another edge case
193+
return buf[size - 1];
194+
} else if (iLow == iHigh) {
195+
return buf[iLow];
196+
} else {
197+
// Interpolate between the two bounding values
198+
return buf[iLow] + (index - iLow) * (buf[iHigh] - buf[iLow]);
199+
}
200+
}
201+
202+
} // DataBuffer
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
*
3+
* Copyright 2013 Netflix, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
package com.netflix.stats.distribution;
19+
20+
21+
/**
22+
* An object that collects new values incrementally.
23+
*
24+
* @author netflixoss
25+
* @version $Revision: $
26+
*/
27+
public interface DataCollector {
28+
29+
/**
30+
* Adds a value to the collected data.
31+
* This must run very quickly, and so can safely
32+
* be called in time-critical code.
33+
*/
34+
void noteValue(double val);
35+
36+
} // DataCollector

0 commit comments

Comments
 (0)