|
| 1 | +/** |
| 2 | + * Copyright (c) 2015 YCSB contributors. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you |
| 5 | + * may not use this file except in compliance with the License. You |
| 6 | + * may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 13 | + * implied. See the License for the specific language governing |
| 14 | + * permissions and limitations under the License. See accompanying |
| 15 | + * LICENSE file. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.yahoo.ycsb.db; |
| 19 | + |
| 20 | +import net.spy.memcached.ConnectionFactoryBuilder; |
| 21 | +import net.spy.memcached.FailureMode; |
| 22 | + |
| 23 | +import java.net.InetSocketAddress; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | + |
| 27 | +/** |
| 28 | + * Concrete Memcached client implementation. |
| 29 | + */ |
| 30 | +public class MemcachedClient extends MemcachedCompatibleClient { |
| 31 | + |
| 32 | + public static final String HOSTS_PROPERTY = "memcached.hosts"; |
| 33 | + |
| 34 | + public static final int DEFAULT_PORT = 11211; |
| 35 | + |
| 36 | + public static final String READ_BUFFER_SIZE_PROPERTY = |
| 37 | + "memcached.readBufferSize"; |
| 38 | + public static final String DEFAULT_READ_BUFFER_SIZE = "3000000"; |
| 39 | + |
| 40 | + public static final String OP_TIMEOUT_PROPERTY = "memcached.opTimeoutMillis"; |
| 41 | + public static final String DEFAULT_OP_TIMEOUT = "60000"; |
| 42 | + |
| 43 | + public static final String FAILURE_MODE_PROPERTY = "memcached.failureMode"; |
| 44 | + public static final FailureMode FAILURE_MODE_PROPERTY_DEFAULT = |
| 45 | + FailureMode.Redistribute; |
| 46 | + |
| 47 | + @Override |
| 48 | + protected net.spy.memcached.MemcachedClient createMemcachedClient() |
| 49 | + throws Exception { |
| 50 | + ConnectionFactoryBuilder connectionFactoryBuilder = |
| 51 | + new ConnectionFactoryBuilder(); |
| 52 | + |
| 53 | + connectionFactoryBuilder.setReadBufferSize(Integer.parseInt( |
| 54 | + getProperties().getProperty(READ_BUFFER_SIZE_PROPERTY, |
| 55 | + DEFAULT_READ_BUFFER_SIZE))); |
| 56 | + |
| 57 | + connectionFactoryBuilder.setOpTimeout(Integer.parseInt( |
| 58 | + getProperties().getProperty(OP_TIMEOUT_PROPERTY, DEFAULT_OP_TIMEOUT))); |
| 59 | + |
| 60 | + String failureString = getProperties().getProperty(FAILURE_MODE_PROPERTY); |
| 61 | + connectionFactoryBuilder.setFailureMode( |
| 62 | + failureString == null ? FAILURE_MODE_PROPERTY_DEFAULT |
| 63 | + : FailureMode.valueOf(failureString)); |
| 64 | + |
| 65 | + // Note: this only works with IPv4 addresses due to its assumption of |
| 66 | + // ":" being the separator of hostname/IP and port; this is not the case |
| 67 | + // when dealing with IPv6 addresses. |
| 68 | + // |
| 69 | + // TODO(mbrukman): fix this. |
| 70 | + List<InetSocketAddress> addresses = new ArrayList<InetSocketAddress>(); |
| 71 | + String[] hosts = getProperties().getProperty(HOSTS_PROPERTY).split(","); |
| 72 | + for (String address : hosts) { |
| 73 | + int colon = address.indexOf(":"); |
| 74 | + int port = DEFAULT_PORT; |
| 75 | + String host = address; |
| 76 | + if (colon != -1) { |
| 77 | + port = Integer.parseInt(address.substring(colon + 1)); |
| 78 | + host = address.substring(0, colon); |
| 79 | + } |
| 80 | + addresses.add(new InetSocketAddress(host, port)); |
| 81 | + } |
| 82 | + return new net.spy.memcached.MemcachedClient( |
| 83 | + connectionFactoryBuilder.build(), addresses); |
| 84 | + } |
| 85 | +} |
0 commit comments