1 | package net.spy.memcached; |
2 | |
3 | import java.io.IOException; |
4 | import java.net.InetSocketAddress; |
5 | import java.net.SocketAddress; |
6 | import java.nio.channels.SocketChannel; |
7 | import java.util.Collection; |
8 | import java.util.Collections; |
9 | import java.util.List; |
10 | import java.util.concurrent.ArrayBlockingQueue; |
11 | import java.util.concurrent.BlockingQueue; |
12 | import java.util.concurrent.LinkedBlockingQueue; |
13 | |
14 | import net.spy.memcached.compat.SpyObject; |
15 | import net.spy.memcached.ops.Operation; |
16 | import net.spy.memcached.protocol.ascii.AsciiMemcachedNodeImpl; |
17 | import net.spy.memcached.protocol.ascii.AsciiOperationFactory; |
18 | import net.spy.memcached.protocol.binary.BinaryMemcachedNodeImpl; |
19 | import net.spy.memcached.protocol.binary.BinaryOperationFactory; |
20 | import net.spy.memcached.transcoders.SerializingTranscoder; |
21 | import net.spy.memcached.transcoders.Transcoder; |
22 | |
23 | /** |
24 | * Default implementation of ConnectionFactory. |
25 | * |
26 | * <p> |
27 | * This implementation creates connections where the operation queue is an |
28 | * ArrayBlockingQueue and the read and write queues are unbounded |
29 | * LinkedBlockingQueues. The <code>Redistribute</code> FailureMode is used |
30 | * by default. |
31 | * </p> |
32 | */ |
33 | public class DefaultConnectionFactory extends SpyObject |
34 | implements ConnectionFactory { |
35 | |
36 | /** |
37 | * Default failure mode. |
38 | */ |
39 | public static final FailureMode DEFAULT_FAILURE_MODE = |
40 | FailureMode.Redistribute; |
41 | |
42 | /** |
43 | * Default hash algorithm. |
44 | */ |
45 | public static final HashAlgorithm DEFAULT_HASH = HashAlgorithm.NATIVE_HASH; |
46 | |
47 | /** |
48 | * Maximum length of the operation queue returned by this connection |
49 | * factory. |
50 | */ |
51 | public static final int DEFAULT_OP_QUEUE_LEN=16384; |
52 | |
53 | /** |
54 | * The read buffer size for each server connection from this factory. |
55 | */ |
56 | public static final int DEFAULT_READ_BUFFER_SIZE=16384; |
57 | |
58 | /** |
59 | * Default operation timeout in milliseconds. |
60 | */ |
61 | public static final long DEFAULT_OPERATION_TIMEOUT = 1000; |
62 | |
63 | /** |
64 | * Maximum amount of time (in seconds) to wait between reconnect attempts. |
65 | */ |
66 | public static final long DEFAULT_MAX_RECONNECT_DELAY = 30; |
67 | |
68 | private final int opQueueLen; |
69 | private final int readBufSize; |
70 | private final HashAlgorithm hashAlg; |
71 | |
72 | /** |
73 | * Construct a DefaultConnectionFactory with the given parameters. |
74 | * |
75 | * @param qLen the queue length. |
76 | * @param bufSize the buffer size |
77 | * @param hash the algorithm to use for hashing |
78 | */ |
79 | public DefaultConnectionFactory(int qLen, int bufSize, HashAlgorithm hash) { |
80 | super(); |
81 | opQueueLen=qLen; |
82 | readBufSize=bufSize; |
83 | hashAlg=hash; |
84 | } |
85 | |
86 | /** |
87 | * Create a DefaultConnectionFactory with the given maximum operation |
88 | * queue length, and the given read buffer size. |
89 | */ |
90 | public DefaultConnectionFactory(int qLen, int bufSize) { |
91 | this(qLen, bufSize, DEFAULT_HASH); |
92 | } |
93 | |
94 | /** |
95 | * Create a DefaultConnectionFactory with the default parameters. |
96 | */ |
97 | public DefaultConnectionFactory() { |
98 | this(DEFAULT_OP_QUEUE_LEN, DEFAULT_READ_BUFFER_SIZE); |
99 | } |
100 | |
101 | public MemcachedNode createMemcachedNode(SocketAddress sa, |
102 | SocketChannel c, int bufSize) { |
103 | |
104 | OperationFactory of = getOperationFactory(); |
105 | if(of instanceof AsciiOperationFactory) { |
106 | return new AsciiMemcachedNodeImpl(sa, c, bufSize, |
107 | createReadOperationQueue(), |
108 | createWriteOperationQueue(), |
109 | createOperationQueue()); |
110 | } else if(of instanceof BinaryOperationFactory) { |
111 | return new BinaryMemcachedNodeImpl(sa, c, bufSize, |
112 | createReadOperationQueue(), |
113 | createWriteOperationQueue(), |
114 | createOperationQueue()); |
115 | } else { |
116 | throw new IllegalStateException( |
117 | "Unhandled operation factory type " + of); |
118 | } |
119 | } |
120 | |
121 | /* (non-Javadoc) |
122 | * @see net.spy.memcached.ConnectionFactory#createConnection(java.util.List) |
123 | */ |
124 | public MemcachedConnection createConnection(List<InetSocketAddress> addrs) |
125 | throws IOException { |
126 | return new MemcachedConnection(getReadBufSize(), this, addrs, |
127 | getInitialObservers(), getFailureMode(), getOperationFactory()); |
128 | } |
129 | |
130 | /* (non-Javadoc) |
131 | * @see net.spy.memcached.ConnectionFactory#getFailureMode() |
132 | */ |
133 | public FailureMode getFailureMode() { |
134 | return DEFAULT_FAILURE_MODE; |
135 | } |
136 | |
137 | /* (non-Javadoc) |
138 | * @see net.spy.memcached.ConnectionFactory#createOperationQueue() |
139 | */ |
140 | public BlockingQueue<Operation> createOperationQueue() { |
141 | return new ArrayBlockingQueue<Operation>(getOpQueueLen()); |
142 | } |
143 | |
144 | /* (non-Javadoc) |
145 | * @see net.spy.memcached.ConnectionFactory#createReadOperationQueue() |
146 | */ |
147 | public BlockingQueue<Operation> createReadOperationQueue() { |
148 | return new LinkedBlockingQueue<Operation>(); |
149 | } |
150 | |
151 | /* (non-Javadoc) |
152 | * @see net.spy.memcached.ConnectionFactory#createWriteOperationQueue() |
153 | */ |
154 | public BlockingQueue<Operation> createWriteOperationQueue() { |
155 | return new LinkedBlockingQueue<Operation>(); |
156 | } |
157 | |
158 | /* (non-Javadoc) |
159 | * @see net.spy.memcached.ConnectionFactory#createLocator(java.util.List) |
160 | */ |
161 | public NodeLocator createLocator(List<MemcachedNode> nodes) { |
162 | return new ArrayModNodeLocator(nodes, getHashAlg()); |
163 | } |
164 | |
165 | /** |
166 | * Get the op queue length set at construct time. |
167 | */ |
168 | public int getOpQueueLen() { |
169 | return opQueueLen; |
170 | } |
171 | |
172 | /* (non-Javadoc) |
173 | * @see net.spy.memcached.ConnectionFactory#getReadBufSize() |
174 | */ |
175 | public int getReadBufSize() { |
176 | return readBufSize; |
177 | } |
178 | |
179 | /* (non-Javadoc) |
180 | * @see net.spy.memcached.ConnectionFactory#getHashAlg() |
181 | */ |
182 | public HashAlgorithm getHashAlg() { |
183 | return hashAlg; |
184 | } |
185 | |
186 | /* (non-Javadoc) |
187 | * @see net.spy.memcached.ConnectionFactory#getOperationFactory() |
188 | */ |
189 | public OperationFactory getOperationFactory() { |
190 | return new AsciiOperationFactory(); |
191 | } |
192 | |
193 | /* (non-Javadoc) |
194 | * @see net.spy.memcached.ConnectionFactory#getOperationTimeout() |
195 | */ |
196 | public long getOperationTimeout() { |
197 | return DEFAULT_OPERATION_TIMEOUT; |
198 | } |
199 | |
200 | /* (non-Javadoc) |
201 | * @see net.spy.memcached.ConnectionFactory#isDaemon() |
202 | */ |
203 | public boolean isDaemon() { |
204 | return false; |
205 | } |
206 | |
207 | /* (non-Javadoc) |
208 | * @see net.spy.memcached.ConnectionFactory#getInitialObservers() |
209 | */ |
210 | public Collection<ConnectionObserver> getInitialObservers() { |
211 | return Collections.emptyList(); |
212 | } |
213 | |
214 | /* (non-Javadoc) |
215 | * @see net.spy.memcached.ConnectionFactory#getDefaultTranscoder() |
216 | */ |
217 | public Transcoder<Object> getDefaultTranscoder() { |
218 | return new SerializingTranscoder(); |
219 | } |
220 | |
221 | /* (non-Javadoc) |
222 | * @see net.spy.memcached.ConnectionFactory#useNagleAlgorithm() |
223 | */ |
224 | public boolean useNagleAlgorithm() { |
225 | return false; |
226 | } |
227 | |
228 | /* (non-Javadoc) |
229 | * @see net.spy.memcached.ConnectionFactory#shouldOptimize() |
230 | */ |
231 | public boolean shouldOptimize() { |
232 | return true; |
233 | } |
234 | |
235 | /* (non-Javadoc) |
236 | * @see net.spy.memcached.ConnectionFactory#getMaxReconnectDelay() |
237 | */ |
238 | public long getMaxReconnectDelay() { |
239 | return DEFAULT_MAX_RECONNECT_DELAY; |
240 | } |
241 | |
242 | } |