3737 "nullness" // TODO(https://github.com/apache/beam/issues/20497)
3838})
3939public class WindmillWatermarkHold extends WindmillState implements WatermarkHoldState {
40+
4041 // The encoded size of an Instant.
4142 private static final int ENCODED_SIZE = 8 ;
4243
@@ -46,6 +47,7 @@ public class WindmillWatermarkHold extends WindmillState implements WatermarkHol
4647 private final String stateFamily ;
4748
4849 private boolean cleared = false ;
50+ private boolean knownEmpty = false ;
4951 /**
5052 * If non-{@literal null}, the known current hold value, or absent if we know there are no output
5153 * watermark holds. If {@literal null}, the current hold value could depend on holds in Windmill
@@ -81,6 +83,7 @@ public void clear() {
8183 public void setKnownEmpty () {
8284 cachedValue = Optional .absent ();
8385 localAdditions = null ;
86+ knownEmpty = true ;
8487 }
8588
8689 @ Override
@@ -139,11 +142,13 @@ public Future<Windmill.WorkItemCommitRequest> persist(
139142
140143 Future <Windmill .WorkItemCommitRequest > result ;
141144
142- if (!cleared && localAdditions == null ) {
145+ if (!knownEmpty && ! cleared && localAdditions == null ) {
143146 // No changes, so no need to update Windmill and no need to cache any value.
144147 return Futures .immediateFuture (Windmill .WorkItemCommitRequest .newBuilder ().buildPartial ());
145148 }
146149
150+ final int estimatedByteSize = ENCODED_SIZE + stateKey .byteString ().size ();
151+
147152 if (cleared && localAdditions == null ) {
148153 // Just clearing the persisted state; blind delete
149154 Windmill .WorkItemCommitRequest .Builder commitBuilder =
@@ -172,15 +177,17 @@ public Future<Windmill.WorkItemCommitRequest> persist(
172177 } else if (!cleared && localAdditions != null ) {
173178 // Otherwise, we need to combine the local additions with the already persisted data
174179 result = combineWithPersisted ();
180+ } else if (knownEmpty ) {
181+ result = Futures .immediateFuture (Windmill .WorkItemCommitRequest .newBuilder ().buildPartial ());
175182 } else {
176183 throw new IllegalStateException ("Unreachable condition" );
177184 }
178185
179- final int estimatedByteSize = ENCODED_SIZE + stateKey .byteString ().size ();
180186 return Futures .lazyTransform (
181187 result ,
182188 result1 -> {
183189 cleared = false ;
190+ knownEmpty = false ;
184191 localAdditions = null ;
185192 if (cachedValue != null ) {
186193 cache .put (namespace , stateKey , WindmillWatermarkHold .this , estimatedByteSize );
0 commit comments