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 000000000000..10e46cbcc351 --- /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 53480a74f18a..fd9600277d2e 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 000000000000..12a85f9f79a2 --- /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 793f9d1e13f8..9495d5720a36 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 f8e9a4921fbe..24acb1c5e6db 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 000000000000..969f9e97f7b8 --- /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 18044068a60b..1445f38a4f0f 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 000000000000..61736e71a62f --- /dev/null +++ b/drools-reliability/drools-reliability-infinispan/src/main/java/org/drools/reliability/infinispan/proto/ProtoStreamStoredEvent.java @@ -0,0 +1,111 @@ +/* + * 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 java.io.UncheckedIOException; + +import org.drools.reliability.core.BaseStoredEvent; +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; + +/** + * 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 String typeUrl; + private final transient AnySchema.Any protoObject; + + public ProtoStreamStoredEvent(Object object, boolean propagated, long timestamp, long duration) { + super(propagated, timestamp, duration); + + 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); + } + + @ProtoFactory + public ProtoStreamStoredEvent(AnySchema.Any protoObject, boolean propagated, long timestamp, long duration) { + super(propagated, timestamp, duration); + + 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); + } + } + + @ProtoField(value = 1, required = true) + public AnySchema.Any getProtoObject() { + return protoObject; + } + + @Override + @ProtoField(value = 2, required = true) + 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; + } + + @Override + public Object getObject() { + return object; + } + + @Override + public String toString() { + return "ProtoStreamStoredObject{" + + "object=" + object + + ", propagated=" + propagated + + ", timestamp=" + timestamp + + ", duration=" + duration + + ", typeUrl='" + typeUrl + '\'' + + '}'; + } +} \ 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 afc82bf824c6..43c1537a6744 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 @@ -40,11 +40,7 @@ public class ProtoStreamStoredObject extends BaseStoredObject { 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; @@ -60,8 +56,8 @@ public ProtoStreamStoredObject(Object object, boolean propagated, long timestamp } @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(); @@ -86,21 +82,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,9 +92,6 @@ public String toString() { return "ProtoStreamStoredObject{" + "object=" + object + ", propagated=" + propagated + - ", timestamp=" + timestamp + - ", duration=" + duration + - ", handleId=" + handleId + ", typeUrl='" + typeUrl + '\'' + '}'; } 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 b6540cdcd7a5..e2eb1872db12 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); } }