---
title: " MetaQ技术内幕——源码分析(七)"
author: "wbj0110"
date: "2014-05-31T14:03:30Z"
tags: ["metaq"]
categories: ["编程语言"]
slug: "metaq"
draft: false
lastmod: "2016-03-12T12:29:05Z"
id: 2074344
url: "https://soledede.iteye.com/blog/2074344"
---

前面介绍了Broker在网络传输过程中使用的数据结构，同时也介绍了MetaQ使用了Gecko框架作为网络传输框架。

有人会问，Gecko什么调用MetaEncodeCommand的encode()方法，让命令变成可见的明文在网络传输，Gecko又在什么时候将网络传输的数据包装成一个个Command对象？

或许有人已经注意到了笔者在介绍Broker启动类MetaMorphosisBroker的时候估计漏掉了一个方法newRemotingServer()方法，即创建Gecko Server。

Java代码 [![复制代码](http://udukwilliam.iteye.com/images/icon_copy.gif)](http://udukwilliam.iteye.com/blog/1935740 "复制代码") [![收藏代码](http://udukwilliam.iteye.com/images/icon_star.png)![](http://udukwilliam.iteye.com/images/spinner.gif)](http://blog.csdn.net/qiaochao911/article/details/23441651 "收藏这段代码")

1. private static RemotingServer newRemotingServer(final MetaConfig metaConfig) {
2. final ServerConfig serverConfig = new ServerConfig();
3. serverConfig.setWireFormatType(new MetamorphosisWireFormatType()); //注册了MetamorphosisWireFormatType实例，该实例负责编码和解码Command
4. serverConfig.setPort(metaConfig.getServerPort());
5. final RemotingServer server = RemotingFactory.newRemotingServer(serverConfig);
6. return server;
7. }

```
private static RemotingServer newRemotingServer(final MetaConfig metaConfig) {		final ServerConfig serverConfig = new ServerConfig();		serverConfig.setWireFormatType(new MetamorphosisWireFormatType()); //注册了MetamorphosisWireFormatType实例，该实例负责编码和解码Command		serverConfig.setPort(metaConfig.getServerPort());		final RemotingServer server = RemotingFactory.newRemotingServer(serverConfig);		return server;}
```

在该方法内注册了一个MetamorphosisWireFormatType实例，该实例负责Command 的编码解码工作，MetamorphosisWireFormatType实现接口WireFormatType。

Java代码 [![复制代码](http://udukwilliam.iteye.com/images/icon_copy.gif)](http://udukwilliam.iteye.com/blog/1935740 "复制代码") [![收藏代码](http://udukwilliam.iteye.com/images/icon_star.png)![](http://udukwilliam.iteye.com/images/spinner.gif)](http://blog.csdn.net/qiaochao911/article/details/23441651 "收藏这段代码")

01. public class MetamorphosisWireFormatType extends WireFormatType {
02. public static final String SCHEME = "meta";
03. public String getScheme() {
04. return SCHEME;
05. }
06. public String name() {
07. return "metamorphosis";
08. }
09. public CodecFactory newCodecFactory() {
10. return new MetaCodecFactory();
11. }
12. public CommandFactory newCommandFactory() {
13. return new MetaCommandFactory();
14. }

```
public class MetamorphosisWireFormatType extends WireFormatType {	public static final String SCHEME = "meta";	public String getScheme() {		return SCHEME;	}	public String name() {		return "metamorphosis";	}	public CodecFactory newCodecFactory() {		return new MetaCodecFactory();	}	public CommandFactory newCommandFactory() {		return new MetaCommandFactory();	}
```

MetamorphosisWireFormatType本身并没有进行编码解码，而是交给了类MetaCodecFactory去实现，另外我们也看到newCommandFactory()方法，该方法主要是用于连接的心跳检测。下面让我们分别来看看这两个类: MetaCommandFactory和MetaCodecFactory，MetaCommandFactory和MetaCodecFactory均是MetamorphosisWireFormatType的内部类

用于心跳检测的类MetaCommandFactory，该类主要有两个方法，创建心跳请求的createHeartBeatCommand()方法和响应心跳请求的createBooleanAckCommand()方法：

Java代码 [![复制代码](http://udukwilliam.iteye.com/images/icon_copy.gif)](http://udukwilliam.iteye.com/blog/1935740 "复制代码") [![收藏代码](http://udukwilliam.iteye.com/images/icon_star.png)![](http://udukwilliam.iteye.com/images/spinner.gif)](http://blog.csdn.net/qiaochao911/article/details/23441651 "收藏这段代码")

01. static class MetaCommandFactory implements CommandFactory {
02. public BooleanAckCommand createBooleanAckCommand(final CommandHeader request, final ResponseStatus responseStatus, final String errorMsg) {
03. //响应心跳请求
04. int httpCode = -1;
05. switch (responseStatus) {
06. case NO\_ERROR:
07. httpCode = HttpStatus.Success;
08. break;
09. case THREADPOOL\_BUSY:
10. case NO\_PROCESSOR:
11. httpCode = HttpStatus.ServiceUnavilable;
12. break;
13. case TIMEOUT:
14. httpCode = HttpStatus.GatewayTimeout;
15. break;
16. default:
17. httpCode = HttpStatus.InternalServerError;
18. break;
19. }
20. return new BooleanCommand(httpCode, errorMsg, request.getOpaque());
21. }
22. public HeartBeatRequestCommand createHeartBeatCommand() {
23. //前面介绍过VersionCommand用于心跳检测，就是用于此处
24. return new VersionCommand(OpaqueGenerator.getNextOpaque());
25. }
26. }

```
static class MetaCommandFactory implements CommandFactory {		public BooleanAckCommand createBooleanAckCommand(final CommandHeader request, final ResponseStatus responseStatus, final String errorMsg) {//响应心跳请求			int httpCode = -1;			switch (responseStatus) {				case NO_ERROR:					httpCode = HttpStatus.Success;					break;				case THREADPOOL_BUSY:				case NO_PROCESSOR:					httpCode = HttpStatus.ServiceUnavilable;					break;				case TIMEOUT:					httpCode = HttpStatus.GatewayTimeout;					break;				default:					httpCode = HttpStatus.InternalServerError;					break;			}			return new BooleanCommand(httpCode, errorMsg, request.getOpaque());		}		public HeartBeatRequestCommand createHeartBeatCommand() {//前面介绍过VersionCommand用于心跳检测，就是用于此处			return new VersionCommand(OpaqueGenerator.getNextOpaque());		}	}
```

MetaCodecFactory是MetaQ（包括Broker和Client，因为编码解码Broker和Client都需要）网络传输最重要的一个类，负责命令的编码解码，MetaCodecFactory要实现Gecko框架定义的接口CodecFactory，MetaCodecFactory实例才能被Gecko框架使用，接口CodecFactory就定义了两个方法，返回编码器和解码器（由于Client和Broker均需要使用到MetamorphosisWireFormatType，所以MetamorphosisWireFormatType放在common工程中）：

Java代码 [![复制代码](http://udukwilliam.iteye.com/images/icon_copy.gif)](http://udukwilliam.iteye.com/blog/1935740 "复制代码") [![收藏代码](http://udukwilliam.iteye.com/images/icon_star.png)![](http://udukwilliam.iteye.com/images/spinner.gif)](http://blog.csdn.net/qiaochao911/article/details/23441651 "收藏这段代码")

001. static class MetaCodecFactory implements CodecFactory {
002. //返回解码器
003. @Override
004. public Decoder getDecoder() {
005. return new Decoder() {
006. //Gecko框架会在适当的时候调用该方法，并将数据放到参数buff中，
007. //用户可以根据buff的内容进行解析，包装成对应的Command类型
008. public Object decode(final IoBuffer buff, final Session session) {
009. if (buff == null || !buff.hasRemaining()) {
010. return null;
011. }
012. buff.mark();
013. //匹配第一个{‘\\r’, ‘\\n’}，也就是找到命令的内容(不包括数据)，目前只有PutCommand和SynCommand有数据部分，其他的命令都只有命令的内容
014. final int index = LINE\_MATCHER.matchFirst(buff);
015. if (index &gt;= 0) {
016. //获取命令内容
017. final byte\[] bytes = new byte\[index - buff.position()];
018. buff.get(bytes);
019. //跳过\\r\\n
020. buff.position(buff.position() + 2);
021. //将命令字节数组转换成字符串
022. final String line = ByteUtils.getString(bytes);
023. if (log.isDebugEnabled()) {
024. log.debug("Receive command:" + line);
025. }
026. //以空格为单位分离内容
027. final String\[] sa = SPLITER.split(line);
028. if (sa == null || sa.length == 0) {
029. throw new MetaCodecException("Blank command line.");
030. }
031. //判断内容的第一个字母
032. final byte op = (byte) sa\[0].charAt(0);
033. switch (op) {
034. case 'p':
035. //如果是p的话，认为是put命令，具体见MetaEncodeCommand定义的命令的内容并解析put命令，具体格式在每个命令的实现类里的注释都有，下面的各个方法的注释也有部分
036. return this.decodePut(buff, sa);
037. case 'g':
038. //如果是g的话，认为是get命令
039. return this.decodeGet(sa);
040. case 't':
041. //如果是g的话，认为是事务命令
042. return this.decodeTransaction(sa);
043. case 'r':
044. //如果是g的话，认为是结果响应
045. return this.decodeBoolean(buff, sa);
046. case 'v':
047. //如果是v的话，则可能是心跳请求或者数据响应，所以得使用更详细的信息进行判断
048. if (sa\[0].equals("value")) {
049. return this.decodeData(buff, sa);
050. } else {
051. return this.decodeVersion(sa);
052. }
053. case 's':
054. //如果是s的话，则可能是统计请求或者同步，所以得使用更详细的信息进行判断
055. if (sa\[0].equals("stats")) {
056. return this.decodeStats(sa);
057. } else {
058. return this.decodeSync(buff, sa);
059. }
060. case 'o':
061. //如果是o的话，查询最近可用位置请求
062. return this.decodeOffset(sa);
063. case 'q':
064. //如果是q的话，退出连接请求
065. return this.decodeQuit();
066. default:
067. throw new MetaCodecException("Unknow command:" + line);
068. }
069. } else {
070. return null;
071. }
072. }
073. private Object decodeQuit() {
074. return new QuitCommand();
075. }
076. private Object decodeVersion(final String\[] sa) {
077. if (sa.length &gt;= 2) {
078. return new VersionCommand(Integer.parseInt(sa\[1]));
079. } else {
080. return new VersionCommand(Integer.MAX\_VALUE);
081. }
082. }
083. // offset topic group partition offset opaque\\r\\n
084. private Object decodeOffset(final String\[] sa) {
085. this.assertCommand(sa\[0], "offset");
086. return new OffsetCommand(sa\[1], sa\[2], Integer.parseInt(sa\[3]), Long.parseLong(sa\[4]), Integer.parseInt(sa\[5]));
087. }
088. // stats item opaque\\r\\n
089. // opaque可以为空
090. private Object decodeStats(final String\[] sa) {
091. this.assertCommand(sa\[0], "stats");
092. int opaque = Integer.MAX\_VALUE;
093. if (sa.length &gt;= 3) {
094. opaque = Integer.parseInt(sa\[2]);
095. }
096. String item = null;
097. if (sa.length &gt;= 2) {
098. item = sa\[1];
099. }
100. return new StatsCommand(opaque, item);
101. }
102. // value totalLen opaque\\r\\n data
103. private Object decodeData(final IoBuffer buff, final String\[] sa) {
104. this.assertCommand(sa\[0], "value");
105. final int valueLen = Integer.parseInt(sa\[1]);
106. if (buff.remaining() &lt; valueLen) {
107. buff.reset();
108. return null;
109. } else {
110. final byte\[] data = new byte\[valueLen];
111. buff.get(data);
112. return new DataCommand(data, Integer.parseInt(sa\[2]));
113. }
114. }
115. /\**
116. \* result code length opaque\\r\\n message
117. \*
118. \* @param buff
119. \* @param sa
120. \* @return
121. \*/
122. private Object decodeBoolean(final IoBuffer buff, final String\[] sa) {
123. this.assertCommand(sa\[0], "result");
124. final int valueLen = Integer.parseInt(sa\[2]);
125. if (valueLen == 0) {
126. return new BooleanCommand(Integer.parseInt(sa\[1]), null, Integer.parseInt(sa\[3]));
127. } else {
128. if (buff.remaining() &lt; valueLen) {
129. buff.reset();
130. return null;
131. } else {
132. final byte\[] data = new byte\[valueLen];
133. buff.get(data);
134. return new BooleanCommand(Integer.parseInt(sa\[1]), ByteUtils.getString(data), Integer.parseInt(sa\[3]));
135. }
136. }
137. }
138. // get topic group partition offset maxSize opaque\\r\\n
139. private Object decodeGet(final String\[] sa) {
140. this.assertCommand(sa\[0], "get");
141. return new GetCommand(sa\[1], sa\[2], Integer.parseInt(sa\[3]), Long.parseLong(sa\[4]), Integer.parseInt(sa\[5]), Integer.parseInt(sa\[6]));
142. }
143. // transaction key sessionId type \[timeout] \[unique qualifier]
144. // opaque\\r\\n
145. private Object decodeTransaction(final String\[] sa) {
146. this.assertCommand(sa\[0], "transaction");
147. final TransactionId transactionId = this.getTransactionId(sa\[1]);
148. final TransactionType type = TransactionType.valueOf(sa\[3]);
149. switch (sa.length) {
150. case 7:
151. // Both include timeout and unique qualifier.
152. int timeout = Integer.valueOf(sa\[4]);
153. String uniqueQualifier = sa\[5];
154. TransactionInfo info = new TransactionInfo(transactionId, sa\[2], type, uniqueQualifier, timeout);
155. return new TransactionCommand(info, Integer.parseInt(sa\[6]));
156. case 6:
157. // Maybe timeout or unique qualifier
158. if (StringUtils.isNumeric(sa\[4])) {
159. timeout = Integer.valueOf(sa\[4]);
160. info = new TransactionInfo(transactionId, sa\[2], type, null, timeout);
161. return new TransactionCommand(info, Integer.parseInt(sa\[5]));
162. } else {
163. uniqueQualifier = sa\[4];
164. info = new TransactionInfo(transactionId, sa\[2], type, uniqueQualifier, 0);
165. return new TransactionCommand(info, Integer.parseInt(sa\[5]));
166. }
167. case 5:
168. // Without timeout and unique qualifier.
169. info = new TransactionInfo(transactionId, sa\[2], type, null);
170. return new TransactionCommand(info, Integer.parseInt(sa\[4]));
171. default:
172. throw new MetaCodecException("Invalid transaction command:" + StringUtils.join(sa));
173. }
174. }
175. private TransactionId getTransactionId(final String s) {
176. return TransactionId.valueOf(s);
177. }
178. // sync topic partition value-length flag msgId
179. // opaque\\r\\n
180. private Object decodeSync(final IoBuffer buff, final String\[] sa) {
181. this.assertCommand(sa\[0], "sync");
182. final int valueLen = Integer.parseInt(sa\[3]);
183. if (buff.remaining() &lt; valueLen) {
184. buff.reset();
185. return null;
186. } else {
187. final byte\[] data = new byte\[valueLen];
188. buff.get(data);
189. switch (sa.length) {
190. case 7:
191. // old master before 1.4.4
192. return new SyncCommand(sa\[1], Integer.parseInt(sa\[2]), data, Integer.parseInt(sa\[4]), Long.valueOf(sa\[5]), -1, Integer.parseInt(sa\[6]));
193. case 8:
194. // new master since 1.4.4
195. return new SyncCommand(sa\[1], Integer.parseInt(sa\[2]), data, Integer.parseInt(sa\[4]), Long.valueOf(sa\[5]), Integer.parseInt(sa\[6]), Integer.parseInt(sa\[7]));
196. default:
197. throw new MetaCodecException("Invalid Sync command:" + StringUtils.join(sa));
198. }
199. }
200. }
201. // put topic partition value-length flag checksum
202. // \[transactionKey]
203. // opaque\\r\\n
204. private Object decodePut(final IoBuffer buff, final String\[] sa) {
205. this.assertCommand(sa\[0], "put");
206. final int valueLen = Integer.parseInt(sa\[3]);
207. if (buff.remaining() &lt; valueLen) {
208. buff.reset();
209. return null;
210. } else {
211. final byte\[] data = new byte\[valueLen];
212. buff.get(data);
213. switch (sa.length) {
214. case 6:
215. // old clients before 1.4.4
216. return new PutCommand(sa\[1], Integer.parseInt(sa\[2]), data, null, Integer.parseInt(sa\[4]), Integer.parseInt(sa\[5]));
217. case 7:
218. // either transaction command or new clients since
219. // 1.4.4
220. String slot = sa\[5];
221. char firstChar = slot.charAt(0);
222. if (Character.isDigit(firstChar) || '-' == firstChar) {
223. // slot is checksum.
224. int checkSum = Integer.parseInt(slot);
225. return new PutCommand(sa\[1], Integer.parseInt(sa\[2]), data, Integer.parseInt(sa\[4]), checkSum, null, Integer.parseInt(sa\[6]));
226. } else {
227. // slot is transaction id.
228. return new PutCommand(sa\[1], Integer.parseInt(sa\[2]), data, this.getTransactionId(slot), Integer.parseInt(sa\[4]), Integer.parseInt(sa\[6]));
229. }
230. case 8:
231. // New clients since 1.4.4
232. // A transaction command
233. return new PutCommand(sa\[1], Integer.parseInt(sa\[2]), data, Integer.parseInt(sa\[4]), Integer.parseInt(sa\[5]), this.getTransactionId(sa\[6]), Integer.parseInt(sa\[7]));
234. default:
235. throw new MetaCodecException("Invalid put command:" + StringUtils.join(sa));
236. }
237. }
238. }
239. private void assertCommand(final String cmd, final String expect) {
240. if (!expect.equals(cmd)) {
241. throw new MetaCodecException("Expect " + expect + ",but was " + cmd);
242. }
243. }
244. };
245. }
246. @Override
247. public Encoder getEncoder() {
248. //返回编码器
249. return new Encoder() {
250. @Override
251. public IoBuffer encode(final Object message, final Session session) {
252. //框架会在适当的时候调用编码器的encode()方法，前面说过如果响应的命令是DataCommand的时候假设不是zeroCopy的话，会出现问题。原因就在这里，因为如果不使用zeroCopy的话，返回给Gecko框架的是一个DataCommand的实例，这时候会调用到此方法，而此方法并没有按照DataCommand的格式进行编码，解码器会识别不了，所以容易出问题
253. return ((MetaEncodeCommand) message).encode();
254. }
255. };
256. }

```
static class MetaCodecFactory implements CodecFactory {	//返回解码器		@Override		public Decoder getDecoder() {			return new Decoder() {				//Gecko框架会在适当的时候调用该方法，并将数据放到参数buff中，                //用户可以根据buff的内容进行解析，包装成对应的Command类型				public Object decode(final IoBuffer buff, final Session session) {					if (buff == null || !buff.hasRemaining()) {						return null;					}					buff.mark();                     //匹配第一个{‘\r’, ‘\n’}，也就是找到命令的内容(不包括数据)，目前只有PutCommand和SynCommand有数据部分，其他的命令都只有命令的内容					final int index = LINE_MATCHER.matchFirst(buff);					if (index >= 0) {                          //获取命令内容						final byte[] bytes = new byte[index - buff.position()];						buff.get(bytes);						//跳过\r\n						buff.position(buff.position() + 2);                          //将命令字节数组转换成字符串						final String line = ByteUtils.getString(bytes);						if (log.isDebugEnabled()) {							log.debug("Receive command:" + line);						}                          //以空格为单位分离内容						final String[] sa = SPLITER.split(line);						if (sa == null || sa.length == 0) {							throw new MetaCodecException("Blank command line.");						}                          //判断内容的第一个字母						final byte op = (byte) sa[0].charAt(0);						switch (op) {							case 'p':                                    //如果是p的话，认为是put命令，具体见MetaEncodeCommand定义的命令的内容并解析put命令，具体格式在每个命令的实现类里的注释都有，下面的各个方法的注释也有部分								return this.decodePut(buff, sa);							case 'g':                                   //如果是g的话，认为是get命令								return this.decodeGet(sa);							case 't':                                   //如果是g的话，认为是事务命令								return this.decodeTransaction(sa);							case 'r':                                   //如果是g的话，认为是结果响应								return this.decodeBoolean(buff, sa);							case 'v':                                     //如果是v的话，则可能是心跳请求或者数据响应，所以得使用更详细的信息进行判断								if (sa[0].equals("value")) {									return this.decodeData(buff, sa);								} else {									return this.decodeVersion(sa);								}							case 's':							   //如果是s的话，则可能是统计请求或者同步，所以得使用更详细的信息进行判断if (sa[0].equals("stats")) {									return this.decodeStats(sa);								} else {									return this.decodeSync(buff, sa);								}							case 'o':                                  //如果是o的话，查询最近可用位置请求								return this.decodeOffset(sa);							case 'q':                                   //如果是q的话，退出连接请求								return this.decodeQuit();							default:								throw new MetaCodecException("Unknow command:" + line);						}					} else {						return null;					}				}				private Object decodeQuit() {					return new QuitCommand();				}				private Object decodeVersion(final String[] sa) {					if (sa.length >= 2) {						return new VersionCommand(Integer.parseInt(sa[1]));					} else {						return new VersionCommand(Integer.MAX_VALUE);					}				}				// offset topic group partition offset opaque\r\n				private Object decodeOffset(final String[] sa) {					this.assertCommand(sa[0], "offset");					return new OffsetCommand(sa[1], sa[2], Integer.parseInt(sa[3]), Long.parseLong(sa[4]), Integer.parseInt(sa[5]));				}				// stats item opaque\r\n				// opaque可以为空				private Object decodeStats(final String[] sa) {					this.assertCommand(sa[0], "stats");					int opaque = Integer.MAX_VALUE;					if (sa.length >= 3) {						opaque = Integer.parseInt(sa[2]);					}					String item = null;					if (sa.length >= 2) {						item = sa[1];					}					return new StatsCommand(opaque, item);				}				// value totalLen opaque\r\n data				private Object decodeData(final IoBuffer buff, final String[] sa) {					this.assertCommand(sa[0], "value");					final int valueLen = Integer.parseInt(sa[1]);					if (buff.remaining() < valueLen) {						buff.reset();						return null;					} else {						final byte[] data = new byte[valueLen];						buff.get(data);						return new DataCommand(data, Integer.parseInt(sa[2]));					}				}				/**				 * result code length opaque\r\n message				 * 				 * @param buff				 * @param sa				 * @return				 */				private Object decodeBoolean(final IoBuffer buff, final String[] sa) {					this.assertCommand(sa[0], "result");					final int valueLen = Integer.parseInt(sa[2]);					if (valueLen == 0) {						return new BooleanCommand(Integer.parseInt(sa[1]), null, Integer.parseInt(sa[3]));					} else {						if (buff.remaining() < valueLen) {							buff.reset();							return null;						} else {							final byte[] data = new byte[valueLen];							buff.get(data);							return new BooleanCommand(Integer.parseInt(sa[1]), ByteUtils.getString(data), Integer.parseInt(sa[3]));						}					}				}				// get topic group partition offset maxSize opaque\r\n				private Object decodeGet(final String[] sa) {					this.assertCommand(sa[0], "get");					return new GetCommand(sa[1], sa[2], Integer.parseInt(sa[3]), Long.parseLong(sa[4]), Integer.parseInt(sa[5]), Integer.parseInt(sa[6]));				}				// transaction key sessionId type [timeout] [unique qualifier]				// opaque\r\n				private Object decodeTransaction(final String[] sa) {					this.assertCommand(sa[0], "transaction");					final TransactionId transactionId = this.getTransactionId(sa[1]);					final TransactionType type = TransactionType.valueOf(sa[3]);					switch (sa.length) {						case 7:							// Both include timeout and unique qualifier.							int timeout = Integer.valueOf(sa[4]);							String uniqueQualifier = sa[5];							TransactionInfo info = new TransactionInfo(transactionId, sa[2], type, uniqueQualifier, timeout);							return new TransactionCommand(info, Integer.parseInt(sa[6]));						case 6:							// Maybe timeout or unique qualifier							if (StringUtils.isNumeric(sa[4])) {								timeout = Integer.valueOf(sa[4]);								info = new TransactionInfo(transactionId, sa[2], type, null, timeout);								return new TransactionCommand(info, Integer.parseInt(sa[5]));							} else {								uniqueQualifier = sa[4];								info = new TransactionInfo(transactionId, sa[2], type, uniqueQualifier, 0);								return new TransactionCommand(info, Integer.parseInt(sa[5]));							}						case 5:							// Without timeout and unique qualifier.							info = new TransactionInfo(transactionId, sa[2], type, null);							return new TransactionCommand(info, Integer.parseInt(sa[4]));						default:							throw new MetaCodecException("Invalid transaction command:" + StringUtils.join(sa));					}				}				private TransactionId getTransactionId(final String s) {					return TransactionId.valueOf(s);				}				// sync topic partition value-length flag msgId				// opaque\r\n				private Object decodeSync(final IoBuffer buff, final String[] sa) {					this.assertCommand(sa[0], "sync");					final int valueLen = Integer.parseInt(sa[3]);					if (buff.remaining() < valueLen) {						buff.reset();						return null;					} else {						final byte[] data = new byte[valueLen];						buff.get(data);						switch (sa.length) {							case 7:								// old master before 1.4.4								return new SyncCommand(sa[1], Integer.parseInt(sa[2]), data, Integer.parseInt(sa[4]), Long.valueOf(sa[5]), -1, Integer.parseInt(sa[6]));							case 8:								// new master since 1.4.4								return new SyncCommand(sa[1], Integer.parseInt(sa[2]), data, Integer.parseInt(sa[4]), Long.valueOf(sa[5]), Integer.parseInt(sa[6]), Integer.parseInt(sa[7]));							default:								throw new MetaCodecException("Invalid Sync command:" + StringUtils.join(sa));						}					}				}				// put topic partition value-length flag checksum				// [transactionKey]				// opaque\r\n				private Object decodePut(final IoBuffer buff, final String[] sa) {					this.assertCommand(sa[0], "put");					final int valueLen = Integer.parseInt(sa[3]);					if (buff.remaining() < valueLen) {						buff.reset();						return null;					} else {						final byte[] data = new byte[valueLen];						buff.get(data);						switch (sa.length) {							case 6:								// old clients before 1.4.4								return new PutCommand(sa[1], Integer.parseInt(sa[2]), data, null, Integer.parseInt(sa[4]), Integer.parseInt(sa[5]));							case 7:								// either transaction command or new clients since								// 1.4.4								String slot = sa[5];								char firstChar = slot.charAt(0);								if (Character.isDigit(firstChar) || '-' == firstChar) {									// slot is checksum.									int checkSum = Integer.parseInt(slot);									return new PutCommand(sa[1], Integer.parseInt(sa[2]), data, Integer.parseInt(sa[4]), checkSum, null, Integer.parseInt(sa[6]));								} else {									// slot is transaction id.									return new PutCommand(sa[1], Integer.parseInt(sa[2]), data, this.getTransactionId(slot), Integer.parseInt(sa[4]), Integer.parseInt(sa[6]));								}							case 8:								// New clients since 1.4.4								// A transaction command								return new PutCommand(sa[1], Integer.parseInt(sa[2]), data, Integer.parseInt(sa[4]), Integer.parseInt(sa[5]), this.getTransactionId(sa[6]), Integer.parseInt(sa[7]));							default:								throw new MetaCodecException("Invalid put command:" + StringUtils.join(sa));						}					}				}				private void assertCommand(final String cmd, final String expect) {					if (!expect.equals(cmd)) {						throw new MetaCodecException("Expect " + expect + ",but was " + cmd);					}				}			};		}		@Override		public Encoder getEncoder() {			//返回编码器return new Encoder() {				@Override				public IoBuffer encode(final Object message, final Session session) {                      //框架会在适当的时候调用编码器的encode()方法，前面说过如果响应的命令是DataCommand的时候假设不是zeroCopy的话，会出现问题。原因就在这里，因为如果不使用zeroCopy的话，返回给Gecko框架的是一个DataCommand的实例，这时候会调用到此方法，而此方法并没有按照DataCommand的格式进行编码，解码器会识别不了，所以容易出问题					return ((MetaEncodeCommand) message).encode();				}			};		}
```

前面还介绍到过MetaMorphosisBroker在启动时会注册请求类型与Processor的映射，见代码：

Java代码 [![复制代码](http://udukwilliam.iteye.com/images/icon_copy.gif)](http://udukwilliam.iteye.com/blog/1935740 "复制代码") [![收藏代码](http://udukwilliam.iteye.com/images/icon_star.png)![](http://udukwilliam.iteye.com/images/spinner.gif)](http://blog.csdn.net/qiaochao911/article/details/23441651 "收藏这段代码")

1. private void registerProcessors() {
2. this.remotingServer.registerProcessor(GetCommand.class, new GetProcessor(this.brokerProcessor, this.executorsManager.getGetExecutor()));
3. this.remotingServer.registerProcessor(PutCommand.class, new PutProcessor(this.brokerProcessor, this.executorsManager.getUnOrderedPutExecutor()));
4. this.remotingServer.registerProcessor(OffsetCommand.class, new OffsetProcessor(this.brokerProcessor, this.executorsManager.getGetExecutor()));
5. this.remotingServer.registerProcessor(HeartBeatRequestCommand.class, new VersionProcessor(this.brokerProcessor));
6. this.remotingServer.registerProcessor(QuitCommand.class, new QuitProcessor(this.brokerProcessor));
7. this.remotingServer.registerProcessor(StatsCommand.class, new StatsProcessor(this.brokerProcessor));
8. this.remotingServer.registerProcessor(TransactionCommand.class, new TransactionProcessor(this.brokerProcessor, this.executorsManager.getUnOrderedPutExecutor()));
9. }

```
private void registerProcessors() {		this.remotingServer.registerProcessor(GetCommand.class, new GetProcessor(this.brokerProcessor, this.executorsManager.getGetExecutor()));		this.remotingServer.registerProcessor(PutCommand.class, new PutProcessor(this.brokerProcessor, this.executorsManager.getUnOrderedPutExecutor()));		this.remotingServer.registerProcessor(OffsetCommand.class, new OffsetProcessor(this.brokerProcessor, this.executorsManager.getGetExecutor()));		this.remotingServer.registerProcessor(HeartBeatRequestCommand.class, new VersionProcessor(this.brokerProcessor));		this.remotingServer.registerProcessor(QuitCommand.class, new QuitProcessor(this.brokerProcessor));		this.remotingServer.registerProcessor(StatsCommand.class, new StatsProcessor(this.brokerProcessor));		this.remotingServer.registerProcessor(TransactionCommand.class, new TransactionProcessor(this.brokerProcessor, this.executorsManager.getUnOrderedPutExecutor()));}
```

依据注册的类型，Gecko框架将会根据解析出来的命令实例调用处理器的不同方法，并返回不同请求的响应。下面让我们来看看不同的处理到底做了些什么事情？因为是Broker针对请求的处理，所以所有的Processor都在server工程中，先上类图：

![](http://dl2.iteye.com/upload/attachment/0088/9091/83632487-90a6-37e6-bd16-356b490ba5a1.jpg)

所有的处理器均实现了RequestProcessor接口，该接口由Gecko框架定义，RequestProcessor类中只定义了两个方法：

Java代码 [![复制代码](http://udukwilliam.iteye.com/images/icon_copy.gif)](http://udukwilliam.iteye.com/blog/1935740 "复制代码") [![收藏代码](http://udukwilliam.iteye.com/images/icon_star.png)![](http://udukwilliam.iteye.com/images/spinner.gif)](http://blog.csdn.net/qiaochao911/article/details/23441651 "收藏这段代码")

01. public interface RequestProcessor&lt;T extends RequestCommand&gt; {
02. /\**
03. \* 处理请求
04. \*
05. \* @param request请求命令
06. \* @param conn 请求来源的连接
07. \*/
08. public void handleRequest(T request, Connection conn);
09. /\**
10. \* 用户自定义的线程池，如果提供，那么请求的处理都将在该线程池内执行
11. \*
12. \* @return
13. \*/
14. public ThreadPoolExecutor getExecutor();
15. }

```
public interface RequestProcessor<T extends RequestCommand> {    /**     * 处理请求     *      * @param request请求命令     * @param conn 请求来源的连接     */    public void handleRequest(T request, Connection conn);    /**     * 用户自定义的线程池，如果提供，那么请求的处理都将在该线程池内执行     *      * @return     */    public ThreadPoolExecutor getExecutor();}
```

所以，加上上一篇文章，我们可以得出MetaQ的大致网络处理流程图解如下：

![](http://dl2.iteye.com/upload/attachment/0088/9095/ece44e3e-0d5a-36e8-848d-4cb63150db79.jpg)

 

 

[http://soledede.com/](http://soledede.com/)

 

**大家可以加我个人微信号：*scccdgf***

 

 

**或者关注soledede的微信公众号：*soledede***

**微信公众号：**

**![](http://dl2.iteye.com/upload/attachment/0109/4338/cacc161d-e6c3-3491-9e7c-f04ad1fec51c.jpg)**