From 396e741b0d62cf33c1f368cc9a4c9ab4fc747c15 Mon Sep 17 00:00:00 2001 From: Toshiya Kobayashi Date: Fri, 7 Jul 2023 16:54:57 +0900 Subject: [PATCH] [DROOLS-7479] Split StoredEvent from StoredFact - removing handleId as no longer used - reduce code duplication --- .../reliability/core/BaseStoredEvent.java | 49 +++++++++++ .../reliability/core/BaseStoredObject.java | 33 +------ .../core/SerializableStoredEvent.java | 46 ++++++++++ .../core/SerializableStoredObject.java | 9 +- ...impleSerializationReliableObjectStore.java | 9 +- .../drools/reliability/core/StoredEvent.java | 28 ++++++ .../drools/reliability/core/StoredObject.java | 8 +- .../proto/ProtoStreamStoredEvent.java | 85 +++++++++++++++++++ .../proto/ProtoStreamStoredObject.java | 59 ++----------- .../infinispan/proto/ProtoStreamUtils.java | 55 ++++++++++++ .../SimpleProtoStreamReliableObjectStore.java | 5 +- 11 files changed, 282 insertions(+), 104 deletions(-) create mode 100644 drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/BaseStoredEvent.java create mode 100644 drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/SerializableStoredEvent.java create mode 100644 drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/StoredEvent.java create mode 100644 drools-reliability/drools-reliability-infinispan/src/main/java/org/drools/reliability/infinispan/proto/ProtoStreamStoredEvent.java create mode 100644 drools-reliability/drools-reliability-infinispan/src/main/java/org/drools/reliability/infinispan/proto/ProtoStreamUtils.java diff --git a/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/BaseStoredEvent.java b/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/BaseStoredEvent.java new file mode 100644 index 00000000000..10e46cbcc35 --- /dev/null +++ b/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/BaseStoredEvent.java @@ -0,0 +1,49 @@ +/* + * Copyright 2023 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.drools.reliability.core; + +import org.drools.core.common.DefaultEventHandle; +import org.drools.core.common.InternalWorkingMemoryEntryPoint; +import org.drools.core.rule.accessor.FactHandleFactory; + +public abstract class BaseStoredEvent extends BaseStoredObject implements StoredEvent { + + protected final long timestamp; + protected final long duration; + + protected BaseStoredEvent(boolean propagated, long timestamp, long duration) { + super(propagated); + this.timestamp = timestamp; + this.duration = duration; + } + + @Override + public long getTimestamp() { + return timestamp; + } + + @Override + public long getDuration() { + return duration; + } + + @Override + public void repropagate(InternalWorkingMemoryEntryPoint ep) { + FactHandleFactory fhFactory = ep.getHandleFactory(); + DefaultEventHandle eFh = fhFactory.createEventFactHandle(fhFactory.getNextId(), getObject(), fhFactory.getNextRecency(), ep, timestamp, duration); + ep.insert(eFh); + } +} \ No newline at end of file diff --git a/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/BaseStoredObject.java b/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/BaseStoredObject.java index 53480a74f18..fd9600277d2 100644 --- a/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/BaseStoredObject.java +++ b/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/BaseStoredObject.java @@ -17,28 +17,15 @@ import java.io.Serializable; -import org.drools.core.common.DefaultEventHandle; import org.drools.core.common.InternalWorkingMemoryEntryPoint; -import org.drools.core.rule.accessor.FactHandleFactory; public abstract class BaseStoredObject implements StoredObject, Serializable { protected final boolean propagated; - protected final long timestamp; - protected final long duration; - protected final long handleId; - protected BaseStoredObject(boolean propagated, long timestamp, long duration, long handleId) { + protected BaseStoredObject(boolean propagated) { this.propagated = propagated; - this.timestamp = timestamp; - this.duration = duration; - this.handleId = handleId; - } - - @Override - public boolean isEvent() { - return timestamp >= 0; } @Override @@ -46,24 +33,8 @@ public boolean isPropagated() { return propagated; } - @Override - public long getTimestamp() { - return timestamp; - } - - @Override - public long getDuration() { - return duration; - } - @Override public void repropagate(InternalWorkingMemoryEntryPoint ep) { - if (isEvent()) { - FactHandleFactory fhFactory = ep.getHandleFactory(); - DefaultEventHandle eFh = fhFactory.createEventFactHandle(fhFactory.getNextId(), getObject(), fhFactory.getNextRecency(), ep, timestamp, duration); - ep.insert(eFh); - } else { - ep.insert(getObject()); - } + ep.insert(getObject()); } } \ No newline at end of file diff --git a/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/SerializableStoredEvent.java b/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/SerializableStoredEvent.java new file mode 100644 index 00000000000..12a85f9f79a --- /dev/null +++ b/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/SerializableStoredEvent.java @@ -0,0 +1,46 @@ +/* + * Copyright 2023 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.drools.reliability.core; + +import java.io.Serializable; + +public class SerializableStoredEvent extends BaseStoredEvent { + + private final Serializable object; + + public SerializableStoredEvent(Object object, boolean propagated, long timestamp, long duration) { + super(propagated, timestamp, duration); + if (!(object instanceof Serializable)) { + throw new IllegalArgumentException("Object must be serializable : " + object.getClass().getCanonicalName()); + } + this.object = (Serializable) object; + } + + @Override + public Serializable getObject() { + return object; + } + + @Override + public String toString() { + return "SerializableStoredEvent{" + + "object=" + object + + ", propagated=" + propagated + + ", timestamp=" + timestamp + + ", duration=" + duration + + '}'; + } +} \ No newline at end of file diff --git a/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/SerializableStoredObject.java b/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/SerializableStoredObject.java index 793f9d1e13f..9495d5720a3 100644 --- a/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/SerializableStoredObject.java +++ b/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/SerializableStoredObject.java @@ -22,11 +22,7 @@ public class SerializableStoredObject extends BaseStoredObject { private final Serializable object; public SerializableStoredObject(Object object, boolean propagated) { - this(object, propagated, -1, -1, -1); - } - - public SerializableStoredObject(Object object, boolean propagated, long timestamp, long duration, long handleId) { - super(propagated, timestamp, duration, handleId); + super(propagated); if (!(object instanceof Serializable)) { throw new IllegalArgumentException("Object must be serializable : " + object.getClass().getCanonicalName()); } @@ -43,9 +39,6 @@ public String toString() { return "SerializableStoredObject{" + "object=" + object + ", propagated=" + propagated + - ", timestamp=" + timestamp + - ", duration=" + duration + - ", handleId=" + handleId + '}'; } } \ No newline at end of file diff --git a/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/SimpleSerializationReliableObjectStore.java b/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/SimpleSerializationReliableObjectStore.java index f8e9a4921fb..24acb1c5e6d 100644 --- a/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/SimpleSerializationReliableObjectStore.java +++ b/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/SimpleSerializationReliableObjectStore.java @@ -86,8 +86,9 @@ private void repropagateWithPseudoClock(InternalWorkingMemory session, InternalW ReliablePseudoClockScheduler clock = (ReliablePseudoClockScheduler) session.getSessionClock(); for (StoredObject storedObject : propagated) { if (storedObject.isEvent()) { + StoredEvent storedEvent = (StoredEvent) storedObject; long currentTime = clock.getCurrentTime(); - long timestamp = storedObject.getTimestamp(); + long timestamp = storedEvent.getTimestamp(); if (currentTime < timestamp) { clock.advanceTime(timestamp - currentTime, TimeUnit.MILLISECONDS); } @@ -114,7 +115,7 @@ public void putIntoPersistedStorage(InternalFactHandle handle, boolean propagate private StoredObject factHandleToStoredObject(InternalFactHandle handle, boolean propagated, Object object) { return handle.isEvent() ? - createStoredObject(propagated, object, ((DefaultEventHandle) handle).getStartTimestamp(), ((DefaultEventHandle) handle).getDuration(), handle.getId()) : + createStoredEvent(propagated, object, ((DefaultEventHandle) handle).getStartTimestamp(), ((DefaultEventHandle) handle).getDuration()) : createStoredObject(propagated, object); } @@ -122,8 +123,8 @@ protected StoredObject createStoredObject(boolean propagated, Object object) { return new SerializableStoredObject(object, propagated); } - protected StoredObject createStoredObject(boolean propagated, Object object, long timestamp, long duration, long handleId) { - return new SerializableStoredObject(object, propagated, timestamp, duration, handleId); + protected StoredEvent createStoredEvent(boolean propagated, Object object, long timestamp, long duration) { + return new SerializableStoredEvent(object, propagated, timestamp, duration); } @Override diff --git a/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/StoredEvent.java b/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/StoredEvent.java new file mode 100644 index 00000000000..969f9e97f7b --- /dev/null +++ b/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/StoredEvent.java @@ -0,0 +1,28 @@ +/* + * Copyright 2023 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.drools.reliability.core; + +public interface StoredEvent extends StoredObject { + + @Override + default boolean isEvent() { + return true; + } + + long getTimestamp(); + + long getDuration(); +} \ No newline at end of file diff --git a/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/StoredObject.java b/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/StoredObject.java index 18044068a60..1445f38a4f0 100644 --- a/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/StoredObject.java +++ b/drools-reliability/drools-reliability-core/src/main/java/org/drools/reliability/core/StoredObject.java @@ -19,15 +19,13 @@ public interface StoredObject { - boolean isEvent(); + default boolean isEvent() { + return false; + } boolean isPropagated(); Object getObject(); - long getTimestamp(); - - long getDuration(); - void repropagate(InternalWorkingMemoryEntryPoint ep); } \ No newline at end of file diff --git a/drools-reliability/drools-reliability-infinispan/src/main/java/org/drools/reliability/infinispan/proto/ProtoStreamStoredEvent.java b/drools-reliability/drools-reliability-infinispan/src/main/java/org/drools/reliability/infinispan/proto/ProtoStreamStoredEvent.java new file mode 100644 index 00000000000..1da273d8cbe --- /dev/null +++ b/drools-reliability/drools-reliability-infinispan/src/main/java/org/drools/reliability/infinispan/proto/ProtoStreamStoredEvent.java @@ -0,0 +1,85 @@ +/* + * Copyright 2023 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.drools.reliability.infinispan.proto; + +import org.drools.reliability.core.BaseStoredEvent; +import org.infinispan.protostream.annotations.ProtoFactory; +import org.infinispan.protostream.annotations.ProtoField; +import org.infinispan.protostream.types.protobuf.AnySchema; + +/** + * This class is used to store objects in Infinispan using ProtoStream. + * This class inherits Serializable from BaseStoredEvent, but it uses ProtoStream instead of Java serialization. + */ +public class ProtoStreamStoredEvent extends BaseStoredEvent { + + private final transient Object object; + + private final transient AnySchema.Any protoObject; + + public ProtoStreamStoredEvent(Object object, boolean propagated, long timestamp, long duration) { + super(propagated, timestamp, duration); + + this.object = object; + this.protoObject = ProtoStreamUtils.toAnySchema(object); + } + + @ProtoFactory + public ProtoStreamStoredEvent(AnySchema.Any protoObject, boolean propagated, long timestamp, long duration) { + super(propagated, timestamp, duration); + + this.protoObject = protoObject; + this.object = ProtoStreamUtils.fromAnySchema(protoObject); + } + + @ProtoField(value = 1, required = true) + public AnySchema.Any getProtoObject() { + return protoObject; + } + + @Override + @ProtoField(value = 2, required = true) + public boolean isPropagated() { + return propagated; + } + + @Override + @ProtoField(value = 3, required = true) + public long getTimestamp() { + return timestamp; + } + + @Override + @ProtoField(value = 4, required = true) + public long getDuration() { + return duration; + } + + @Override + public Object getObject() { + return object; + } + + @Override + public String toString() { + return "ProtoStreamStoredEvent{" + + "object=" + object + + ", propagated=" + propagated + + ", timestamp=" + timestamp + + ", duration=" + duration + + '}'; + } +} \ No newline at end of file diff --git a/drools-reliability/drools-reliability-infinispan/src/main/java/org/drools/reliability/infinispan/proto/ProtoStreamStoredObject.java b/drools-reliability/drools-reliability-infinispan/src/main/java/org/drools/reliability/infinispan/proto/ProtoStreamStoredObject.java index afc82bf824c..8fec498e311 100644 --- a/drools-reliability/drools-reliability-infinispan/src/main/java/org/drools/reliability/infinispan/proto/ProtoStreamStoredObject.java +++ b/drools-reliability/drools-reliability-infinispan/src/main/java/org/drools/reliability/infinispan/proto/ProtoStreamStoredObject.java @@ -15,15 +15,7 @@ package org.drools.reliability.infinispan.proto; -import java.io.IOException; -import java.io.UncheckedIOException; - import org.drools.reliability.core.BaseStoredObject; -import org.drools.reliability.core.ReliabilityRuntimeException; -import org.drools.reliability.core.StorageManagerFactory; -import org.drools.reliability.infinispan.InfinispanStorageManager; -import org.infinispan.protostream.ProtobufUtil; -import org.infinispan.protostream.SerializationContext; import org.infinispan.protostream.annotations.ProtoFactory; import org.infinispan.protostream.annotations.ProtoField; import org.infinispan.protostream.types.protobuf.AnySchema; @@ -36,43 +28,21 @@ public class ProtoStreamStoredObject extends BaseStoredObject { private final transient Object object; - private final String typeUrl; private final transient AnySchema.Any protoObject; public ProtoStreamStoredObject(Object object, boolean propagated) { - this(object, propagated, -1, -1, -1); - } - - public ProtoStreamStoredObject(Object object, boolean propagated, long timestamp, long duration, long handleId) { - super(propagated, timestamp, duration, handleId); + super(propagated); this.object = object; - - this.typeUrl = object.getClass().getCanonicalName(); - SerializationContext serializationContext = ((InfinispanStorageManager) StorageManagerFactory.get().getStorageManager()).getSerializationContext(); - byte[] objectBytes; - try { - objectBytes = ProtobufUtil.toByteArray(serializationContext, object); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - this.protoObject = new AnySchema.Any(typeUrl, objectBytes); + this.protoObject = ProtoStreamUtils.toAnySchema(object); } @ProtoFactory - public ProtoStreamStoredObject(AnySchema.Any protoObject, boolean propagated, long timestamp, long duration, long handleId) { - super(propagated, timestamp, duration, handleId); + public ProtoStreamStoredObject(AnySchema.Any protoObject, boolean propagated) { + super(propagated); this.protoObject = protoObject; - this.typeUrl = protoObject.getTypeUrl(); - - SerializationContext serializationContext = ((InfinispanStorageManager) StorageManagerFactory.get().getStorageManager()).getSerializationContext(); - try { - Class type = Class.forName(this.typeUrl); - this.object = ProtobufUtil.fromByteArray(serializationContext, protoObject.getValue(), type); - } catch (IOException | ClassNotFoundException e) { - throw new ReliabilityRuntimeException(e); - } + this.object = ProtoStreamUtils.fromAnySchema(protoObject); } @ProtoField(value = 1, required = true) @@ -86,21 +56,6 @@ public boolean isPropagated() { return propagated; } - @ProtoField(value = 3, required = true) - public long getTimestamp() { - return timestamp; - } - - @ProtoField(value = 4, required = true) - public long getDuration() { - return duration; - } - - @ProtoField(value = 5, required = true) - public long getHandleId() { - return handleId; - } - @Override public Object getObject() { return object; @@ -111,10 +66,6 @@ public String toString() { return "ProtoStreamStoredObject{" + "object=" + object + ", propagated=" + propagated + - ", timestamp=" + timestamp + - ", duration=" + duration + - ", handleId=" + handleId + - ", typeUrl='" + typeUrl + '\'' + '}'; } } \ No newline at end of file diff --git a/drools-reliability/drools-reliability-infinispan/src/main/java/org/drools/reliability/infinispan/proto/ProtoStreamUtils.java b/drools-reliability/drools-reliability-infinispan/src/main/java/org/drools/reliability/infinispan/proto/ProtoStreamUtils.java new file mode 100644 index 00000000000..af15f73f6e5 --- /dev/null +++ b/drools-reliability/drools-reliability-infinispan/src/main/java/org/drools/reliability/infinispan/proto/ProtoStreamUtils.java @@ -0,0 +1,55 @@ +/* + * Copyright 2023 Red Hat, Inc. and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.drools.reliability.infinispan.proto; + +import java.io.IOException; + +import org.drools.reliability.core.ReliabilityRuntimeException; +import org.drools.reliability.core.StorageManagerFactory; +import org.drools.reliability.infinispan.InfinispanStorageManager; +import org.infinispan.protostream.ProtobufUtil; +import org.infinispan.protostream.SerializationContext; +import org.infinispan.protostream.types.protobuf.AnySchema; + +public class ProtoStreamUtils { + + private ProtoStreamUtils() { + // util class + } + + public static AnySchema.Any toAnySchema(Object object) { + SerializationContext serializationContext = ((InfinispanStorageManager) StorageManagerFactory.get().getStorageManager()).getSerializationContext(); + byte[] objectBytes; + try { + objectBytes = ProtobufUtil.toByteArray(serializationContext, object); + } catch (IOException e) { + throw new ReliabilityRuntimeException(e); + } + return new AnySchema.Any(object.getClass().getCanonicalName(), objectBytes); + } + + public static Object fromAnySchema(AnySchema.Any protoObject) { + final Object object; + SerializationContext serializationContext = ((InfinispanStorageManager) StorageManagerFactory.get().getStorageManager()).getSerializationContext(); + try { + Class type = Class.forName(protoObject.getTypeUrl()); + object = ProtobufUtil.fromByteArray(serializationContext, protoObject.getValue(), type); + } catch (IOException | ClassNotFoundException e) { + throw new ReliabilityRuntimeException(e); + } + return object; + } +} diff --git a/drools-reliability/drools-reliability-infinispan/src/main/java/org/drools/reliability/infinispan/proto/SimpleProtoStreamReliableObjectStore.java b/drools-reliability/drools-reliability-infinispan/src/main/java/org/drools/reliability/infinispan/proto/SimpleProtoStreamReliableObjectStore.java index b6540cdcd7a..e2eb1872db1 100644 --- a/drools-reliability/drools-reliability-infinispan/src/main/java/org/drools/reliability/infinispan/proto/SimpleProtoStreamReliableObjectStore.java +++ b/drools-reliability/drools-reliability-infinispan/src/main/java/org/drools/reliability/infinispan/proto/SimpleProtoStreamReliableObjectStore.java @@ -17,6 +17,7 @@ import org.drools.core.common.Storage; import org.drools.reliability.core.SimpleSerializationReliableObjectStore; +import org.drools.reliability.core.StoredEvent; import org.drools.reliability.core.StoredObject; public class SimpleProtoStreamReliableObjectStore extends SimpleSerializationReliableObjectStore { @@ -35,7 +36,7 @@ protected StoredObject createStoredObject(boolean propagated, Object object) { } @Override - protected StoredObject createStoredObject(boolean propagated, Object object, long timestamp, long duration, long handleId) { - return new ProtoStreamStoredObject(object, propagated, timestamp, duration, handleId); + protected StoredEvent createStoredEvent(boolean propagated, Object object, long timestamp, long duration) { + return new ProtoStreamStoredEvent(object, propagated, timestamp, duration); } }