29
29
abstract class Watcher <T > {
30
30
static final String HAS_NEXT_EXCEPTION_MESSAGE = "IO Exception during hasNext method." ;
31
31
private static final LoggingFacade LOGGER = LoggingFactory .getLogger ("Operator" , "Operator" );
32
- private static final String IGNORED_RESOURCE_VERSION = "0" ;
32
+ private static final long IGNORED_RESOURCE_VERSION = 0 ;
33
33
34
34
private final AtomicBoolean isDraining = new AtomicBoolean (false );
35
- private String resourceVersion ;
35
+ private Long resourceVersion ;
36
36
private AtomicBoolean stopping ;
37
37
private WatchListener <T > listener ;
38
38
private Thread thread = null ;
@@ -45,7 +45,8 @@ abstract class Watcher<T> {
45
45
* @param stopping an atomic boolean to watch to determine when to stop the watcher
46
46
*/
47
47
Watcher (String resourceVersion , AtomicBoolean stopping ) {
48
- this .resourceVersion = resourceVersion ;
48
+ this .resourceVersion =
49
+ !isNullOrEmptyString (resourceVersion ) ? Long .parseLong (resourceVersion ) : 0 ;
49
50
this .stopping = stopping ;
50
51
}
51
52
@@ -110,7 +111,8 @@ protected boolean isStopping() {
110
111
}
111
112
112
113
private void watchForEvents () {
113
- try (WatchI <T > watch = initiateWatch (new WatchBuilder ().withResourceVersion (resourceVersion ))) {
114
+ try (WatchI <T > watch =
115
+ initiateWatch (new WatchBuilder ().withResourceVersion (resourceVersion .toString ()))) {
114
116
while (watch .hasNext ()) {
115
117
Watch .Response <T > item = watch .next ();
116
118
@@ -139,9 +141,11 @@ private boolean isError(Watch.Response<T> item) {
139
141
}
140
142
141
143
private void handleRegularUpdate (Watch .Response <T > item ) {
142
- LOGGER .fine (MessageKeys .WATCH_EVENT , item .type , item .object );
143
- trackResourceVersion (item .type , item .object );
144
- if (listener != null ) listener .receivedResponse (item );
144
+ if (isFresh (item .type , item .object )) {
145
+ LOGGER .fine (MessageKeys .WATCH_EVENT , item .type , item .object );
146
+ trackResourceVersion (item .type , item .object );
147
+ if (listener != null ) listener .receivedResponse (item );
148
+ }
145
149
}
146
150
147
151
private void handleErrorResponse (Watch .Response <T > item ) {
@@ -152,7 +156,8 @@ private void handleErrorResponse(Watch.Response<T> item) {
152
156
if (index1 > 0 ) {
153
157
int index2 = message .indexOf (')' , index1 + 1 );
154
158
if (index2 > 0 ) {
155
- resourceVersion = message .substring (index1 + 1 , index2 );
159
+ String val = message .substring (index1 + 1 , index2 );
160
+ resourceVersion = !isNullOrEmptyString (val ) ? Long .parseLong (val ) : 0 ;
156
161
}
157
162
}
158
163
}
@@ -170,27 +175,34 @@ private void trackResourceVersion(String type, Object object) {
170
175
updateResourceVersion (getNewResourceVersion (type , object ));
171
176
}
172
177
173
- private String getNewResourceVersion (String type , Object object ) {
174
- if (type .equalsIgnoreCase ("DELETED" ))
175
- return Integer .toString (1 + Integer .parseInt (resourceVersion ));
178
+ private long getNewResourceVersion (String type , Object object ) {
179
+ if (type .equalsIgnoreCase ("DELETED" )) return 1 + resourceVersion ;
176
180
else return getResourceVersionFromMetadata (object );
177
181
}
178
182
179
- private String getResourceVersionFromMetadata (Object object ) {
183
+ private long getResourceVersionFromMetadata (Object object ) {
180
184
try {
181
185
Method getMetadata = object .getClass ().getDeclaredMethod ("getMetadata" );
182
186
V1ObjectMeta metadata = (V1ObjectMeta ) getMetadata .invoke (object );
183
- return metadata .getResourceVersion ();
187
+ String val = metadata .getResourceVersion ();
188
+ return !isNullOrEmptyString (val ) ? Long .parseLong (val ) : 0 ;
184
189
} catch (Exception e ) {
185
190
LOGGER .warning (MessageKeys .EXCEPTION , e );
186
191
return IGNORED_RESOURCE_VERSION ;
187
192
}
188
193
}
189
194
190
- private void updateResourceVersion (String newResourceVersion ) {
191
- if (isNullOrEmptyString (resourceVersion )) resourceVersion = newResourceVersion ;
192
- else if (newResourceVersion .compareTo (resourceVersion ) > 0 )
193
- resourceVersion = newResourceVersion ;
195
+ private boolean isFresh (String type , Object object ) {
196
+ if (resourceVersion == 0 ) return true ;
197
+ long newResourceVersion = getResourceVersionFromMetadata (object );
198
+ return type .equalsIgnoreCase ("DELETED" )
199
+ ? newResourceVersion >= resourceVersion
200
+ : newResourceVersion > resourceVersion ;
201
+ }
202
+
203
+ private void updateResourceVersion (long newResourceVersion ) {
204
+ if (resourceVersion == 0 ) resourceVersion = newResourceVersion ;
205
+ else if (newResourceVersion > resourceVersion ) resourceVersion = newResourceVersion ;
194
206
}
195
207
196
208
private static boolean isNullOrEmptyString (String s ) {
0 commit comments