-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve the performance of composing client metrics in liConsumer (#161)
Signed-off-by: Radai Rosenblatt <[email protected]>
- Loading branch information
1 parent
174bc75
commit 2cbbfdc
Showing
9 changed files
with
389 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...che-kafka-clients/src/main/java/com/linkedin/kafka/clients/utils/CompositeCollection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright 2019 LinkedIn Corp. Licensed under the BSD 2-Clause License (the "License"). See License in the project root for license information. | ||
*/ | ||
|
||
package com.linkedin.kafka.clients.utils; | ||
|
||
import java.util.Collection; | ||
import java.util.Iterator; | ||
|
||
/** | ||
* quick and simple unmodifiable implementation of a collection on top of a pair of other collections. | ||
* @param <T> value type | ||
*/ | ||
public class CompositeCollection<T> implements Collection<T> { | ||
private final Collection<T> a; | ||
private final Collection<T> b; | ||
|
||
public CompositeCollection(Collection<T> a, Collection<T> b) { | ||
if (a == null || b == null) { | ||
throw new IllegalArgumentException("arguments must not be null"); | ||
} | ||
this.a = a; | ||
this.b = b; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return a.size() + b.size(); | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return a.isEmpty() && b.isEmpty(); | ||
} | ||
|
||
@Override | ||
public boolean contains(Object o) { | ||
return a.contains(o) || b.contains(o); | ||
} | ||
|
||
@Override | ||
public Iterator<T> iterator() { | ||
return new CompositeIterator<>(a.iterator(), b.iterator()); | ||
} | ||
|
||
@Override | ||
public Object[] toArray() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public <U> U[] toArray(U[] a) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean add(T t) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean remove(Object o) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean containsAll(Collection<?> c) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean addAll(Collection<? extends T> c) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean removeAll(Collection<?> c) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean retainAll(Collection<?> c) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...pache-kafka-clients/src/main/java/com/linkedin/kafka/clients/utils/CompositeIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 2019 LinkedIn Corp. Licensed under the BSD 2-Clause License (the "License"). See License in the project root for license information. | ||
*/ | ||
|
||
package com.linkedin.kafka.clients.utils; | ||
|
||
import java.util.Iterator; | ||
|
||
/** | ||
* quick and simple unmodifiable implementation of an iterator on top of a pair of other iterators. | ||
* @param <T> value type | ||
*/ | ||
public class CompositeIterator<T> implements Iterator<T> { | ||
private final Iterator<T> a; | ||
private final Iterator<T> b; | ||
|
||
public CompositeIterator(Iterator<T> a, Iterator<T> b) { | ||
if (a == null || b == null) { | ||
throw new IllegalArgumentException("arguments must not be null"); | ||
} | ||
this.a = a; | ||
this.b = b; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return a.hasNext() || b.hasNext(); | ||
} | ||
|
||
@Override | ||
public T next() { | ||
if (a.hasNext()) { | ||
return a.next(); | ||
} | ||
return b.next(); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
li-apache-kafka-clients/src/main/java/com/linkedin/kafka/clients/utils/CompositeMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright 2019 LinkedIn Corp. Licensed under the BSD 2-Clause License (the "License"). See License in the project root for license information. | ||
*/ | ||
|
||
package com.linkedin.kafka.clients.utils; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
|
||
/** | ||
* quick and simple unmodifiable implementation of a map on top of a pair of other maps. | ||
* note that this class is meant for a particular use case (composition of kafka client metrics) | ||
* and is written to be fast over perfectly correct | ||
* @param <K> key type | ||
* @param <V> value type | ||
*/ | ||
public class CompositeMap<K, V> implements Map<K, V> { | ||
private final Map<K, V> a; | ||
private final Map<K, V> b; | ||
|
||
public CompositeMap(Map<K, V> a, Map<K, V> b) { | ||
if (a == null || a.isEmpty() || b == null || b.isEmpty()) { | ||
throw new IllegalArgumentException("arguments must be non empty"); | ||
} | ||
this.a = a; | ||
this.b = b; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return a.size() + b.size(); //we assume they're foreign | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return a.isEmpty() && b.isEmpty(); | ||
} | ||
|
||
@Override | ||
public boolean containsKey(Object key) { | ||
return a.containsKey(key) || b.containsKey(key); | ||
} | ||
|
||
@Override | ||
public boolean containsValue(Object value) { | ||
return a.containsValue(value) || b.containsValue(value); | ||
} | ||
|
||
@Override | ||
public V get(Object key) { | ||
//this assumes no map container a null value (and is faster than a containsKey + get) | ||
V v = a.get(key); | ||
if (v != null) { | ||
return v; | ||
} | ||
return b.get(key); | ||
} | ||
|
||
@Override | ||
public V put(K key, V value) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public V remove(Object key) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void putAll(Map<? extends K, ? extends V> m) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Set<K> keySet() { | ||
return new CompositeSet<>(a.keySet(), b.keySet()); | ||
} | ||
|
||
@Override | ||
public Collection<V> values() { | ||
return new CompositeCollection<>(a.values(), b.values()); | ||
} | ||
|
||
@Override | ||
public Set<Entry<K, V>> entrySet() { | ||
return new CompositeSet<>(a.entrySet(), b.entrySet()); | ||
} | ||
} |
Oops, something went wrong.