Skip to content

Commit

Permalink
#813 Add Id and InstanceGuid to Fiber
Browse files Browse the repository at this point in the history
  • Loading branch information
zhabis committed Feb 15, 2023
1 parent 9d5cbc0 commit adf278a
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 13 deletions.
9 changes: 8 additions & 1 deletion src/Azos.Sky.Server/Fabric/Server/FiberMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ public FiberMemory(
int version,
MemoryStatus status,
FiberId id,
Guid instanceGuid,
Guid imageTypeId,
EntityId? impersonateAs,
byte[] fiberParameters,
Atom currentStep,
KeyValuePair<Atom, byte[]>[] slots) :
this(version, status, id, imageTypeId, impersonateAs, PackBuffer(fiberParameters, currentStep, slots)) { }
this(version, status, id, instanceGuid, imageTypeId, impersonateAs, PackBuffer(fiberParameters, currentStep, slots)) { }

/// <summary>
/// .ctor used by unit tests - obtains packed payload
Expand All @@ -64,13 +65,15 @@ public FiberMemory(
int version,
MemoryStatus status,
FiberId id,
Guid instanceGuid,
Guid imageTypeId,
EntityId? impersonateAs,
byte[] buffer)
{
m_Version = version.IsTrue(v => v <= Constraints.MEMORY_FORMAT_VERSION);
m_Status = status;
m_Id = id;
m_InstanceGuid = instanceGuid;
m_ImageTypeId = imageTypeId;
m_ImpersonateAs = impersonateAs;
m_Buffer = buffer.NonNull(nameof(buffer));
Expand All @@ -94,6 +97,7 @@ public FiberMemory(BixReader reader)
reader.ReadAtom(),
reader.ReadGDID());

m_InstanceGuid = reader.ReadGuid();
m_ImageTypeId = reader.ReadGuid();
m_ImpersonateAs = reader.ReadNullableEntityId();
m_Buffer = reader.ReadBuffer();
Expand All @@ -115,6 +119,7 @@ public void WriteOneWay(BixWriter writer)
writer.Write(m_Id.MemoryShard);
writer.Write(m_Id.Gdid);

writer.Write(m_InstanceGuid);
writer.Write(m_ImageTypeId);
writer.Write(m_ImpersonateAs);
writer.WriteBuffer(m_Buffer);
Expand All @@ -123,6 +128,7 @@ public void WriteOneWay(BixWriter writer)
private int m_Version;
private MemoryStatus m_Status;
private FiberId m_Id;
private Guid m_InstanceGuid;
private Guid m_ImageTypeId;
private EntityId? m_ImpersonateAs;
private byte[] m_Buffer;
Expand All @@ -131,6 +137,7 @@ public void WriteOneWay(BixWriter writer)
public int Version => m_Version;
public MemoryStatus Status => m_Status;
public FiberId Id => m_Id;
public Guid InstanceGuid => m_InstanceGuid;
public Guid ImageTypeId => m_ImageTypeId;
public EntityId? ImpersonateAs => m_ImpersonateAs;
public byte[] Buffer => m_Buffer;
Expand Down
2 changes: 1 addition & 1 deletion src/Azos.Sky.Server/Fabric/Server/FiberProcessorDaemon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ private void reportUnknownImage(Guid guid)
fiber = (Fiber)Serialization.SerializationUtils.MakeNewObjectInstance(tFiber);

//Init inject
fiber.__processor__ctor(m_Runtime, fiberParameters, fiberState);
fiber.__processor__ctor(m_Runtime, memory.Id, memory.InstanceGuid, fiberParameters, fiberState);
}
catch(Exception allocationError)
{
Expand Down
19 changes: 17 additions & 2 deletions src/Azos.Sky/Fabric/Fiber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ public abstract class Fiber
/// framework internal method used by processor runtime to initialize fibers.
/// we don't want to have senseless constructor chaining which just creates meaningless code
/// </summary>
internal void __processor__ctor(IFiberRuntime runtime, FiberParameters pars, FiberState state)
internal void __processor__ctor(IFiberRuntime runtime, FiberId id, Guid instanceGuid, FiberParameters pars, FiberState state)
{
m_Id = id;
m_InstanceGuid = instanceGuid;
m_Runtime = runtime.NonNull(nameof(runtime));
m_Parameters = pars.NonNull(nameof(pars));
m_State = state.NonNull(nameof(state));
Expand All @@ -53,10 +55,23 @@ internal void __processor__ctor(IFiberRuntime runtime, FiberParameters pars, Fib
runtime.App.DependencyInjector.InjectInto(this);
}

private FiberId m_Id;
private Guid m_InstanceGuid;
private IFiberRuntime m_Runtime;
private FiberParameters m_Parameters;
private FiberState m_State;


/// <summary>
/// Id of the executing fiber instance
/// </summary>
public FiberId Id => m_Id;

/// <summary>
/// Guid identifying this fiber instance
/// </summary>
public Guid InstanceGuid => m_InstanceGuid;

/// <summary>
/// References the runtime system such as the hosting process and other services which
/// make this fiber execute its code
Expand Down Expand Up @@ -97,7 +112,7 @@ public Guid WriteLog(Azos.Log.MessageType type,
var ff = $"{GetType().DisplayNameWithExpandedGenericArgs()}(`{State.CurrentStep}`)";
from = from.IsNotNullOrWhiteSpace() ? $"{ff}.{from}" : ff;

return rtc.WriteLog(type, from, text, error, related, pars, file, src);
return rtc.WriteLog(type, from, text, error, related ?? InstanceGuid, pars, file, src);
}


Expand Down
2 changes: 1 addition & 1 deletion src/Azos.Sky/Fabric/FiberInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public sealed class FiberInfo : TransientModel
public FiberId Id { get; set; }

[Field(Required = true, Description = "Fiber Guid used for log and other correlation token")]
public Guid FiberGuid { get; set; }
public Guid InstanceGuid { get; set; }

[Field(Required = true,
Description = "Defines what cloud origin (cluster partition) this fiber belongs to and runs")]
Expand Down
8 changes: 4 additions & 4 deletions src/testing/Azos.Tests.Unit/Fabric/MemoryDeltaFormatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void Test01_Roundtrip_NextStep_NoResult()
state.AccountNumber = 987654321;
state.SetAttachment("donkey_fun.jpeg", new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 });
state.ResetAllSlotModificationFlags();
var mem = new FiberMemory(1, MemoryStatus.LockedForCaller, fid, Guid.NewGuid(), null, MemoryFormatTests.GetFiberMemoryBuffer(pars, state));//this packs buffer
var mem = new FiberMemory(1, MemoryStatus.LockedForCaller, fid, Guid.NewGuid(), Guid.NewGuid(), null, MemoryFormatTests.GetFiberMemoryBuffer(pars, state));//this packs buffer

using var wscope = BixWriterBufferScope.DefaultCapacity;
mem.WriteOneWay(wscope.Writer);//Serialize by SHARD
Expand Down Expand Up @@ -134,7 +134,7 @@ public void Test02_Roundtrip_FinalStep_WithResult()
state.AccountNumber = 987654321;
state.SetAttachment("hockey_fun.jpeg", new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 });
state.ResetAllSlotModificationFlags();
var mem = new FiberMemory(1, MemoryStatus.LockedForCaller, fid, Guid.NewGuid(), null, MemoryFormatTests.GetFiberMemoryBuffer(pars, state));//this packs buffer
var mem = new FiberMemory(1, MemoryStatus.LockedForCaller, fid, Guid.NewGuid(), Guid.NewGuid(), null, MemoryFormatTests.GetFiberMemoryBuffer(pars, state));//this packs buffer

using var wscope = BixWriterBufferScope.DefaultCapacity;
mem.WriteOneWay(wscope.Writer);//Serialize by SHARD
Expand Down Expand Up @@ -236,7 +236,7 @@ public void Test03_Roundtrip_FinalStep_NoResult()
state.AccountNumber = 987654321;
state.SetAttachment("hockey_fun.jpeg", new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 });
state.ResetAllSlotModificationFlags();
var mem = new FiberMemory(1, MemoryStatus.LockedForCaller, fid, Guid.NewGuid(), null, MemoryFormatTests.GetFiberMemoryBuffer(pars, state));//this packs buffer
var mem = new FiberMemory(1, MemoryStatus.LockedForCaller, fid, Guid.NewGuid(), Guid.NewGuid(), null, MemoryFormatTests.GetFiberMemoryBuffer(pars, state));//this packs buffer

using var wscope = BixWriterBufferScope.DefaultCapacity;
mem.WriteOneWay(wscope.Writer);//Serialize by SHARD
Expand Down Expand Up @@ -339,7 +339,7 @@ public void Test04_Roundtrip_Crash()
state.AccountNumber = 987654321;
state.SetAttachment("hockey_fun.jpeg", new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 });
state.ResetAllSlotModificationFlags();
var mem = new FiberMemory(1, MemoryStatus.LockedForCaller, fid, Guid.NewGuid(), null, MemoryFormatTests.GetFiberMemoryBuffer(pars, state));//this packs buffer
var mem = new FiberMemory(1, MemoryStatus.LockedForCaller, fid, Guid.NewGuid(), Guid.NewGuid(), null, MemoryFormatTests.GetFiberMemoryBuffer(pars, state));//this packs buffer

using var wscope = BixWriterBufferScope.DefaultCapacity;
mem.WriteOneWay(wscope.Writer);//Serialize by SHARD
Expand Down
8 changes: 4 additions & 4 deletions src/testing/Azos.Tests.Unit/Fabric/MemoryFormatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void Test01_RawSerDeser_withoutpack_NoImpersonate()
{
var fid = new FiberId(Atom.Encode("sys"), Atom.Encode("s1"), new GDID(0, 1, 1));
var bin = new byte[] { 1, 2, 3 };
var mem = new FiberMemory(1, MemoryStatus.LockedForCaller, fid, Guid.NewGuid(), null, bin);
var mem = new FiberMemory(1, MemoryStatus.LockedForCaller, fid, Guid.NewGuid(), Guid.NewGuid(), null, bin);

using var wscope = BixWriterBufferScope.DefaultCapacity;

Expand All @@ -56,7 +56,7 @@ public void Test02_RawSerDeser_withoutpack_Impersonate()
var fid = new FiberId(Atom.Encode("sys"), Atom.Encode("s1"), new GDID(0, 1, 1));
var mikoyan = new EntityId(Atom.Encode("idp"), Atom.Encode("id"), Atom.ZERO, "mikoyan.ashot.kalgy");
var bin = new byte[] { 1, 2, 3 };
var mem = new FiberMemory(1, MemoryStatus.LockedForCaller, fid, Guid.NewGuid(), mikoyan, bin);
var mem = new FiberMemory(1, MemoryStatus.LockedForCaller, fid, Guid.NewGuid(), Guid.NewGuid(), mikoyan, bin);

using var wscope = BixWriterBufferScope.DefaultCapacity;

Expand Down Expand Up @@ -109,7 +109,7 @@ public void Test03_RawSerDeser_withSlots()
state.SetAttachment("profilepic.jpg", new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });

//shard calls
var mem = new FiberMemory(1, MemoryStatus.LockedForCaller, fid, Guid.NewGuid(), null, GetFiberMemoryBuffer(pars, state));
var mem = new FiberMemory(1, MemoryStatus.LockedForCaller, fid, Guid.NewGuid(), Guid.NewGuid(), null, GetFiberMemoryBuffer(pars, state));

using var wscope = BixWriterBufferScope.DefaultCapacity;

Expand Down Expand Up @@ -191,7 +191,7 @@ public void Test04_RawSerDeser_Benchmark(int cnt)
state.SetAttachment("profilepic.jpg", new byte[23000]);

//shard calls
var mem = new FiberMemory(1, MemoryStatus.LockedForCaller, fid, Guid.NewGuid(), null, GetFiberMemoryBuffer(pars, state));
var mem = new FiberMemory(1, MemoryStatus.LockedForCaller, fid, Guid.NewGuid(), Guid.NewGuid(), null, GetFiberMemoryBuffer(pars, state));

using var wscope = BixWriterBufferScope.DefaultCapacity;

Expand Down

0 comments on commit adf278a

Please sign in to comment.