protected void doPut(Event event) throws InterruptedException {
channelCounter.incrementEventPutAttemptCount();<pre name="code" class="java"> //estimateEventSize方法返回Event的大小(只是Body的大小,header没有计算在内),并且除以byteCapacitySlotSize后向上取整计算
int eventByteSize = (int)Math.ceil(estimateEventSize(event)/byteCapacitySlotSize);
//添加到队列的尾部,如果超过了容量的限制,则添加失败,抛出ChannelException异常。
if (!putList.offer(event)) {
throw new ChannelException(
"Put queue for MemoryTransaction of capacity " +
putList.size() + " full, consider committing more frequently, " +
"increasing capacity or increasing thread count");
}
//记录Event的byte值
putByteCounter += eventByteSize;
}
2,doTake() 方法是从Channel中获取数据。
protected Event doTake() throws InterruptedException {
channelCounter.incrementEventTakeAttemptCount();
//takeList队列的剩余容量,如果为0,则抛异常
if(takeList.remainingCapacity() == 0) {
throw new ChannelException("Take list for MemoryTransaction, capacity " +
takeList.size() + " full, consider committing more frequently, " +
"increasing capacity, or increasing thread count");
}
//尝试获取许可,如果可以获取到许可的话,证明queue队列有空间,否则返回null
if(!queueStored.tryAcquire(keepAlive, TimeUnit.SECONDS)) {
return null;
}
Event event;
synchronized(queueLock) {
//从queue取出一个Event
event = queue.poll();
}
Preconditions.checkNotNull(event, "Queue.poll returned NULL despite semaphore " +
"signalling existence of entry");
//加入到takeList队列中
takeList.put(event);
//更新takeByteCounter大小
int eventByteSize = (int)Math.ceil(estimateEventSize(event)/byteCapacitySlotSize);
takeByteCounter += eventByteSize;
return event;
}
3,doCommit() 提交
protected void doCommit() throws InterruptedException {
int remainingChange = takeList.size() - putList.size();
//如果takeList更小,说明该MemoryChannel放的数据比取的数据要多,所以需要判断该MemoryChannel是否有空间来放
if(remainingChange < 0) {
//利用该信号量判断是否有剩余空间
if(!bytesRemaining.tryAcquire(putByteCounter, keepAlive,
TimeUnit.SECONDS)) {
throw new ChannelException("Cannot commit transaction. Byte capacity " +
"allocated to store event body " + byteCapacity * byteCapacitySlotSize +
"reached. Please increase heap space/byte capacity allocated to " +
"the channel as the sinks may not be keeping up with the sources");
}
//检查queue队列是否有足够的空间。
if(!queueRemaining.tryAcquire(-remainingChange, keepAlive, TimeUnit.SECONDS)) {
bytesRemaining.release(putByteCounter);
throw new ChannelFullException("Space for commit to queue couldn't be acquired." +
" Sinks are likely not keeping up with sources, or the buffer size is too tight");
}
}
int puts = putList.size();
int takes = takeList.size();
//如果上述两个信号量都有空间的话,那么把putList中的Event放到该MemoryChannel中的queue中。
synchronized(queueLock) {
if(puts > 0 ) {
while(!putList.isEmpty()) {
if(!queue.offer(putList.removeFirst())) {
throw new RuntimeException("Queue add failed, this shouldn't be able to happen");
}
}
}
//最后情况putList和takeList
putList.clear();
takeList.clear();
}
//更新queue大小控制的信号量bytesRemaining
bytesRemaining.release(takeByteCounter);
takeByteCounter = 0;
putByteCounter = 0;
queueStored.release(puts);
//takeList比putList大,说明该MemoryChannel中queue的数量应该是减少了,所以把(takeList-putList)的差值加到信号量queueRemaining
if(remainingChange > 0) {
queueRemaining.release(remainingChange);
}
//更新计数器
if (puts > 0) {
channelCounter.addToEventPutSuccessCount(puts);
}
if (takes > 0) {
channelCounter.addToEventTakeSuccessCount(takes);
}
channelCounter.setChannelSize(queue.size());
}
4,doRollback() 回滚
protected void doRollback() {
//获取takeList的大小,然后bytesRemaining中释放
int takes = takeList.size();
//将takeList中的Event重新放回到queue队列中。
synchronized(queueLock) {
Preconditions.checkState(queue.remainingCapacity() >= takeList.size(), "Not enough space in memory channel " +
"queue to rollback takes. This should never happen, please report");
while(!takeList.isEmpty()) {
queue.addFirst(takeList.removeLast());
}
//最后清空putList
putList.clear();
}
//清空了putList,所以需要把putList占用的空间添加到bytesRemaining中
bytesRemaining.release(putByteCounter);
putByteCounter = 0;
takeByteCounter = 0;
queueStored.release(takes);
channelCounter.setChannelSize(queue.size());
}
MemoryChannel的数据流向: