Zookeeper-watcher机制源码分析(二)

在前一篇文章中Zookeeper-watcher机制源码分析(一)说过Watcher的基本流程,在此文中详细剖析服务端几首请求处理流程。

服务端有一个NettyServerCnxn类,用来处理客户端发送过来的请求 

NettyServerCnxn 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

public void receiveMessage(ChannelBuffer message) {

        try {

            while(message.readable() && !throttled) {

                if (bb != null) { //ByteBuffer不为空

                    if (LOG.isTraceEnabled()) {

                        LOG.trace("message readable " + message.readableBytes()

                                " bb len " + bb.remaining() + " " + bb);

                        ByteBuffer dat = bb.duplicate();

                        dat.flip();

                        LOG.trace(Long.toHexString(sessionId)

                                " bb 0x"

                                + ChannelBuffers.hexDump(

                                        ChannelBuffers.copiedBuffer(dat)));

                    }

                    //bb剩余空间大于message中可读字节大小

                    if (bb.remaining() > message.readableBytes()) {

                        int newLimit = bb.position() + message.readableBytes();

                        bb.limit(newLimit);

                    }

                    // 将message写入bb中

                    message.readBytes(bb);

                    bb.limit(bb.capacity());

 

                    if (LOG.isTraceEnabled()) {

                        LOG.trace("after readBytes message readable "

                                + message.readableBytes()

                                " bb len " + bb.remaining() + " " + bb);

                        ByteBuffer dat = bb.duplicate();

                        dat.flip();

                        LOG.trace("after readbytes "

                                + Long.toHexString(sessionId)

                                " bb 0x"

                                + ChannelBuffers.hexDump(

                                        ChannelBuffers.copiedBuffer(dat)));

                    }

                    if (bb.remaining() == 0) { // 已经读完message,表示内容已经全部接收

                        packetReceived(); // 统计接收信息

                        bb.flip();

 

                        ZooKeeperServer zks = this.zkServer;

                        if (zks == null || !zks.isRunning()) { //Zookeeper服务器为空 ,说明服务端挂了

                            throw new IOException("ZK down");

                        }

                        if (initialized) {

                            //处理客户端传过来的数据包

                            zks.processPacket(this, bb);

 

                            if (zks.shouldThrottle(outstandingCount.incrementAndGet())) {

                                disableRecvNoWait();

                            }

                        else {

                            LOG.debug("got conn req request from "

                                    + getRemoteSocketAddress());

                            zks.processConnectRequest(this, bb);

                            initialized = true;

                        }

                        bb = null;

                    }

                else //bb为null的情况,大家自己去看,我就不细讲了

                    if (LOG.isTraceEnabled()) {

                        LOG.trace("message readable "

                                + message.readableBytes()

                                " bblenrem " + bbLen.remaining());

                        ByteBuffer dat = bbLen.duplicate();

                        dat.flip();

                        LOG.trace(Long.toHexString(sessionId)

                                " bbLen 0x"

                                + ChannelBuffers.hexDump(

                                        ChannelBuffers.copiedBuffer(dat)));

                    }

 

                    if (message.readableBytes() < bbLen.remaining()) {

                        bbLen.limit(bbLen.position() + message.readableBytes());

                    }

                    message.readBytes(bbLen);

                    bbLen.limit(bbLen.capacity());

                    if (bbLen.remaining() == 0) {

                        bbLen.flip();

 

                        if (LOG.isTraceEnabled()) {

                            LOG.trace(Long.toHexString(sessionId)

                                    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

训练营资料福利官

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值