@@ -38,7 +38,8 @@ public interface ObjectWriter {
38
38
// TODO: first n bit is the compressed flag
39
39
byte DATA_BLOCK_DEFAULT_FLAG = 0x02 ;
40
40
41
- static ObjectWriter writer (long objectId , ObjectStorage objectStorage , int blockSizeThreshold , int partSizeThreshold ) {
41
+ static ObjectWriter writer (long objectId , ObjectStorage objectStorage , int blockSizeThreshold ,
42
+ int partSizeThreshold ) {
42
43
return new DefaultObjectWriter (objectId , objectStorage , blockSizeThreshold , partSizeThreshold );
43
44
}
44
45
@@ -79,11 +80,14 @@ class DefaultObjectWriter implements ObjectWriter {
79
80
private IndexBlock indexBlock ;
80
81
private long size ;
81
82
83
+ private long lastStreamId = Constants .NOOP_STREAM_ID ;
84
+ private long lastEndOffset = Constants .NOOP_OFFSET ;
85
+
82
86
/**
83
87
* Create a new object writer.
84
88
*
85
89
* @param objectId object id
86
- * @param objectStorage S3 operator
90
+ * @param objectStorage S3 operator
87
91
* @param blockSizeThreshold the max size of a block
88
92
* @param partSizeThreshold the max size of a part. If it is smaller than {@link Writer#MIN_PART_SIZE}, it will be set to {@link Writer#MIN_PART_SIZE}.
89
93
*/
@@ -98,6 +102,7 @@ public DefaultObjectWriter(long objectId, ObjectStorage objectStorage, int block
98
102
}
99
103
100
104
public synchronized void write (long streamId , List <StreamRecordBatch > records ) {
105
+ check (streamId , records );
101
106
List <List <StreamRecordBatch >> blocks = groupByBlock (records );
102
107
for (List <StreamRecordBatch > blockRecords : blocks ) {
103
108
DataBlock block = new DataBlock (streamId , blockRecords );
@@ -109,6 +114,32 @@ public synchronized void write(long streamId, List<StreamRecordBatch> records) {
109
114
}
110
115
}
111
116
117
+ private void check (long streamId , List <StreamRecordBatch > records ) {
118
+ if (records .isEmpty ()) {
119
+ return ;
120
+ }
121
+ long recordsEndOffset = records .get (records .size () - 1 ).getLastOffset ();
122
+ if (lastStreamId == Constants .NOOP_STREAM_ID ) {
123
+ lastStreamId = streamId ;
124
+ lastEndOffset = recordsEndOffset ;
125
+ return ;
126
+ }
127
+ if (lastStreamId > streamId ) {
128
+ throw new IllegalArgumentException (String .format ("The incoming streamId=%s is less than last streamId=%s" , streamId , lastStreamId ));
129
+ } else if (lastStreamId == streamId ) {
130
+ long recordsStartOffset = records .get (0 ).getBaseOffset ();
131
+ if (recordsStartOffset < lastEndOffset ) {
132
+ throw new IllegalArgumentException (String .format ("The incoming streamId=%s startOffset=%s is less than lastEndOffset=%s" ,
133
+ streamId , recordsStartOffset , lastEndOffset ));
134
+ } else {
135
+ lastEndOffset = recordsEndOffset ;
136
+ }
137
+ } else {
138
+ lastStreamId = streamId ;
139
+ lastEndOffset = recordsEndOffset ;
140
+ }
141
+ }
142
+
112
143
private List <List <StreamRecordBatch >> groupByBlock (List <StreamRecordBatch > records ) {
113
144
List <List <StreamRecordBatch >> blocks = new LinkedList <>();
114
145
List <StreamRecordBatch > blockRecords = new ArrayList <>(records .size ());
0 commit comments