-
Notifications
You must be signed in to change notification settings - Fork 315
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
add etcd watch implementation #45
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c5b17e8
add etcd watch implementation
StupidHod 177de5b
change gRPC class to package class
StupidHod 8d2baa5
add final for watch stub
StupidHod 5e16176
add closeable for Watcher, change visible for EtcdUtil
StupidHod 20c19b5
change EtcdWatchImpl constract interface
StupidHod File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.coreos.jetcd; | ||
|
||
import com.coreos.jetcd.api.Event; | ||
import com.coreos.jetcd.api.ResponseHeader; | ||
import com.coreos.jetcd.data.ByteSequence; | ||
import com.coreos.jetcd.data.EtcdHeader; | ||
import com.coreos.jetcd.data.KeyValue; | ||
import com.coreos.jetcd.watch.WatchEvent; | ||
import com.google.protobuf.ByteString; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* This util is to convert api class to client class. | ||
*/ | ||
class EtcdUtil { | ||
|
||
private EtcdUtil() { | ||
} | ||
|
||
/** | ||
* convert ByteSequence to ByteString | ||
*/ | ||
protected static ByteString byteStringFromByteSequence(ByteSequence byteSequence) { | ||
return ByteString.copyFrom(byteSequence.getBytes()); | ||
} | ||
|
||
/** | ||
* convert ByteString to ByteSequence | ||
* @return | ||
*/ | ||
protected static ByteSequence byteSequceFromByteString(ByteString byteString) { | ||
return ByteSequence.fromBytes(byteString.toByteArray()); | ||
} | ||
|
||
/** | ||
* convert API KeyValue to etcd client KeyValue | ||
*/ | ||
protected static KeyValue apiToClientKV(com.coreos.jetcd.api.KeyValue keyValue) { | ||
return new KeyValue( | ||
byteSequceFromByteString(keyValue.getKey()), | ||
byteSequceFromByteString(keyValue.getValue()), | ||
keyValue.getCreateRevision(), | ||
keyValue.getModRevision(), | ||
keyValue.getVersion(), | ||
keyValue.getLease()); | ||
} | ||
|
||
/** | ||
* convert API watch event to etcd client event | ||
*/ | ||
protected static WatchEvent apiToClientEvent(Event event) { | ||
WatchEvent.EventType eventType = WatchEvent.EventType.UNRECOGNIZED; | ||
switch (event.getType()) { | ||
case DELETE: | ||
eventType = WatchEvent.EventType.DELETE; | ||
break; | ||
case PUT: | ||
eventType = WatchEvent.EventType.PUT; | ||
break; | ||
} | ||
return new WatchEvent(apiToClientKV(event.getKv()), apiToClientKV(event.getPrevKv()), eventType); | ||
} | ||
|
||
protected static List<WatchEvent> apiToClientEvents(List<Event> events) { | ||
List<WatchEvent> watchEvents = new ArrayList<>(); | ||
for (Event event : events) { | ||
watchEvents.add(apiToClientEvent(event)); | ||
} | ||
return watchEvents; | ||
} | ||
|
||
/** | ||
* convert API response header to self defined header | ||
*/ | ||
protected static EtcdHeader apiToClientHeader(ResponseHeader header, long compactRevision) { | ||
return new EtcdHeader(header.getClusterId(), header.getMemberId(), header.getRevision(), header.getRaftTerm(), compactRevision); | ||
} | ||
} |
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,74 @@ | ||
package com.coreos.jetcd; | ||
|
||
import com.coreos.jetcd.data.ByteSequence; | ||
import com.coreos.jetcd.data.EtcdHeader; | ||
import com.coreos.jetcd.options.WatchOption; | ||
import com.coreos.jetcd.watch.WatchEvent; | ||
|
||
import java.io.Closeable; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* Interface of watch client | ||
*/ | ||
public interface EtcdWatch { | ||
|
||
|
||
/** | ||
* Watch watches on a key or prefix. The watched events will be called by onWatch. | ||
* If the watch is slow or the required rev is compacted, the watch request | ||
* might be canceled from the server-side and the onCreateFailed will be called. | ||
* | ||
* @param key the key subscribe | ||
* @param watchOption key option | ||
* @param callback call back | ||
* @return ListenableFuture watcher | ||
*/ | ||
CompletableFuture<Watcher> watch(ByteSequence key, WatchOption watchOption, WatchCallback callback); | ||
|
||
interface Watcher extends Closeable { | ||
|
||
/** | ||
* get watcher id | ||
* | ||
* @return id | ||
*/ | ||
long getWatchID(); | ||
|
||
long getLastRevision(); | ||
|
||
ByteSequence getKey(); | ||
|
||
boolean isResuming(); | ||
|
||
/** | ||
* get the watch option | ||
* | ||
* @return watch option | ||
*/ | ||
WatchOption getWatchOption(); | ||
|
||
/** | ||
* cancel the watcher | ||
* | ||
* @return cancel result | ||
*/ | ||
CompletableFuture<Boolean> cancel(); | ||
} | ||
|
||
interface WatchCallback { | ||
|
||
/** | ||
* onWatch will be called when watcher receive any events | ||
* | ||
* @param events received events | ||
*/ | ||
void onWatch(EtcdHeader header, List<WatchEvent> events); | ||
|
||
/** | ||
* onResuming will be called when the watcher is on resuming. | ||
*/ | ||
void onResuming(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would implement also Closeable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to close.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep but having it as Closeable would make it easy to close it along with other non etcd resources and not having to do something specific, Closeable could be implemented as default method wrapping cancel
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, you are right, I will do it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a big issue indeed ;)
Just trying to make my future life easy ;)