Skip to content

Commit

Permalink
MessageBatch: determineMode() is not O(n) anymore, but O(1) (https://…
Browse files Browse the repository at this point in the history
  • Loading branch information
belaban committed Aug 27, 2024
1 parent 97ab64f commit 76998c2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 37 deletions.
45 changes: 13 additions & 32 deletions src/org/jgroups/util/MessageBatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class MessageBatch implements Iterable<Message> {
protected boolean multicast;

/** Whether this message batch contains only OOB messages, or only regular messages */
protected Mode mode=Mode.REG;
protected Mode mode;

/** For benchmarking; may get removed without notice */
protected long timestamp; // ns
Expand Down Expand Up @@ -83,10 +83,11 @@ public MessageBatch(Address dest, Address sender, AsciiString cluster_name, bool
public boolean isMulticast() {return multicast;}
public boolean multicast() {return multicast;}
public MessageBatch multicast(boolean flag) {multicast=flag; return this;}
public Mode getMode() {return mode;}
public MessageBatch mcast(boolean flag) {multicast=flag; return this;}
public Mode getMode() {return mode();}
public Mode mode() {return mode;}
public MessageBatch setMode(Mode mode) {this.mode=mode; return this;}
public MessageBatch mode(Mode mode) {this.mode=mode; return this;}
public MessageBatch setMode(Mode m) {return mode(m);}
public MessageBatch mode(Mode m) {if(mode == null) mode=m; return this;}
public int capacity() {return messages.capacity();}
public long timestamp() {return timestamp;}
public MessageBatch timestamp(long ts) {timestamp=ts; return this;}
Expand Down Expand Up @@ -124,7 +125,7 @@ public MessageBatch add(final Message msg) {
public int add(final Message msg, boolean resize) {
int added=messages.add(msg, resize);
if(added > 0)
determineMode(msg);
determineMode();
return added;
}

Expand Down Expand Up @@ -278,7 +279,8 @@ public String toString() {
sb.append("dest=" + dest);
if(sender != null)
sb.append(", sender=").append(sender);
sb.append(", mode=" + mode);
if(mode != null)
sb.append(", mode=" + mode);
if(cluster_name != null)
sb.append(", cluster=").append(cluster_name);
if(sb.length() > 0)
Expand All @@ -301,34 +303,13 @@ public String printHeaders() {



public enum Mode {OOB, REG, MIXED}
public enum Mode {OOB, REG}

protected MessageBatch determineMode() {
boolean first=true;
for(Iterator<Message> it=messages.iterator(); it.hasNext();) {
Message msg=it.next();
if(first) {
mode=msg.isFlagSet(Message.Flag.OOB)? Mode.OOB : Mode.REG;
first=false;
continue;
}
if(mode == Mode.REG && msg.isFlagSet(Message.Flag.OOB) || mode == Mode.OOB && !msg.isFlagSet(Message.Flag.OOB))
return setMode(Mode.MIXED);
}
return this;
}

protected MessageBatch determineMode(Message msg) {
if(msg == null) return this;
if(messages.size() <= 1)
mode=msg.isFlagSet(Message.Flag.OOB)? Mode.OOB : Mode.REG;
else {
if(mode == Mode.REG && msg.isFlagSet(Message.Flag.OOB)
|| mode == Mode.OOB && !msg.isFlagSet(Message.Flag.OOB))
return setMode(Mode.MIXED);
}
return this;
if(mode != null || messages.isEmpty())
return this;
Message first=messages.get(0);
return mode(first.isFlagSet(Message.Flag.OOB)? Mode.OOB : Mode.REG);
}


}
10 changes: 5 additions & 5 deletions tests/junit-functional/org/jgroups/tests/MessageBatchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -643,20 +643,20 @@ public void testDetermineMode() {
for(int i=0; i < 3; i++)
l.add(new EmptyMessage(null).setFlag(Message.Flag.OOB));
MessageBatch batch=new MessageBatch(null, null, new AsciiString("cluster"), true, l);
assert batch.getMode() == MessageBatch.Mode.OOB;
assert batch.mode() == MessageBatch.Mode.OOB;
batch.add(new EmptyMessage(null));
assert batch.getMode() == MessageBatch.Mode.MIXED;
assert batch.mode() == MessageBatch.Mode.OOB; // mode cannot be changed after it is set the first time

batch=new MessageBatch(3);
batch.add(new EmptyMessage(null));
assert batch.getMode() == MessageBatch.Mode.REG;
assert batch.mode() == MessageBatch.Mode.REG;
batch.add(new EmptyMessage(null).setFlag(Message.Flag.OOB));
assert batch.getMode() == MessageBatch.Mode.MIXED;
assert batch.mode() == MessageBatch.Mode.REG;

batch=new MessageBatch(3);
l.add(new EmptyMessage(null));
batch.add(l);
assert batch.getMode() == MessageBatch.Mode.MIXED;
assert batch.mode() == MessageBatch.Mode.OOB;
}

public void testShuffle() {
Expand Down

0 comments on commit 76998c2

Please sign in to comment.