Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 16 | package org.onlab.util; |
| 17 | |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 18 | import com.esotericsoftware.kryo.Kryo; |
HIGUCHI Yuta | 0a1f29e | 2016-05-05 15:34:41 -0700 | [diff] [blame] | 19 | import com.esotericsoftware.kryo.Registration; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 20 | import com.esotericsoftware.kryo.Serializer; |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 21 | import com.esotericsoftware.kryo.io.ByteBufferInput; |
| 22 | import com.esotericsoftware.kryo.io.ByteBufferOutput; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 23 | import com.esotericsoftware.kryo.io.Input; |
Yuta HIGUCHI | 86f142f | 2016-07-09 17:44:09 -0700 | [diff] [blame] | 24 | import com.esotericsoftware.kryo.io.Output; |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 25 | import com.esotericsoftware.kryo.pool.KryoCallback; |
Yuta HIGUCHI | 633cf88 | 2014-10-20 09:10:28 -0700 | [diff] [blame] | 26 | import com.esotericsoftware.kryo.pool.KryoFactory; |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 27 | import com.esotericsoftware.kryo.pool.KryoPool; |
Jordan Halterman | c10a501 | 2017-06-12 10:34:42 -0700 | [diff] [blame] | 28 | import com.esotericsoftware.kryo.serializers.CompatibleFieldSerializer; |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 29 | import com.google.common.base.MoreObjects; |
Jordan Halterman | e1558e8 | 2017-06-13 14:38:20 -0700 | [diff] [blame] | 30 | import com.google.common.base.Strings; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 31 | import com.google.common.collect.ImmutableList; |
Jonathan Hart | be093f7 | 2016-03-25 11:14:29 -0700 | [diff] [blame] | 32 | import org.apache.commons.lang3.tuple.Pair; |
| 33 | import org.objenesis.strategy.StdInstantiatorStrategy; |
| 34 | import org.slf4j.Logger; |
| 35 | |
Jordan Halterman | ecfca4f | 2017-08-03 21:38:02 -0700 | [diff] [blame] | 36 | import java.io.ByteArrayInputStream; |
| 37 | import java.io.ByteArrayOutputStream; |
Jonathan Hart | be093f7 | 2016-03-25 11:14:29 -0700 | [diff] [blame] | 38 | import java.io.InputStream; |
| 39 | import java.io.OutputStream; |
| 40 | import java.nio.ByteBuffer; |
| 41 | import java.util.ArrayList; |
Jordan Halterman | 0d1f3fa | 2017-05-26 00:14:27 -0700 | [diff] [blame] | 42 | import java.util.Arrays; |
Jonathan Hart | be093f7 | 2016-03-25 11:14:29 -0700 | [diff] [blame] | 43 | import java.util.List; |
HIGUCHI Yuta | b2d0fd8 | 2016-05-17 20:34:58 -0700 | [diff] [blame] | 44 | import java.util.Objects; |
Jonathan Hart | be093f7 | 2016-03-25 11:14:29 -0700 | [diff] [blame] | 45 | |
HIGUCHI Yuta | b2d0fd8 | 2016-05-17 20:34:58 -0700 | [diff] [blame] | 46 | import static com.google.common.base.Preconditions.checkNotNull; |
Jonathan Hart | be093f7 | 2016-03-25 11:14:29 -0700 | [diff] [blame] | 47 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 48 | |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Pool of Kryo instances, with classes pre-registered. |
| 51 | */ |
| 52 | //@ThreadSafe |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 53 | public final class KryoNamespace implements KryoFactory, KryoPool { |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Default buffer size used for serialization. |
| 57 | * |
| 58 | * @see #serialize(Object) |
| 59 | */ |
Yuta HIGUCHI | 3878205 | 2014-11-09 23:51:58 -0800 | [diff] [blame] | 60 | public static final int DEFAULT_BUFFER_SIZE = 4096; |
Yuta HIGUCHI | d2a3882 | 2014-11-06 19:05:04 -0800 | [diff] [blame] | 61 | public static final int MAX_BUFFER_SIZE = 100 * 1000 * 1000; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 62 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 63 | /** |
| 64 | * ID to use if this KryoNamespace does not define registration id. |
| 65 | */ |
| 66 | public static final int FLOATING_ID = -1; |
| 67 | |
| 68 | /** |
| 69 | * Smallest ID free to use for user defined registrations. |
| 70 | */ |
HIGUCHI Yuta | 0a1f29e | 2016-05-05 15:34:41 -0700 | [diff] [blame] | 71 | public static final int INITIAL_ID = 16; |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 72 | |
HIGUCHI Yuta | b2d0fd8 | 2016-05-17 20:34:58 -0700 | [diff] [blame] | 73 | private static final String NO_NAME = "(no name)"; |
| 74 | |
HIGUCHI Yuta | b49b007 | 2016-02-22 22:50:45 -0800 | [diff] [blame] | 75 | private static final Logger log = getLogger(KryoNamespace.class); |
| 76 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 77 | private final KryoPool pool = new KryoPool.Builder(this) |
| 78 | .softReferences() |
| 79 | .build(); |
| 80 | |
| 81 | private final ImmutableList<RegistrationBlock> registeredBlocks; |
| 82 | |
Jordan Halterman | e1558e8 | 2017-06-13 14:38:20 -0700 | [diff] [blame] | 83 | private final boolean compatible; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 84 | private final boolean registrationRequired; |
HIGUCHI Yuta | b2d0fd8 | 2016-05-17 20:34:58 -0700 | [diff] [blame] | 85 | private final String friendlyName; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 86 | |
| 87 | /** |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 88 | * KryoNamespace builder. |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 89 | */ |
| 90 | //@NotThreadSafe |
| 91 | public static final class Builder { |
Jordan Halterman | e1558e8 | 2017-06-13 14:38:20 -0700 | [diff] [blame] | 92 | private static final String ISSU_PROPERTY_NAME = "onos.cluster.issu.enabled"; |
| 93 | private static final boolean DEFAULT_ISSU_ENABLED = false; |
| 94 | |
| 95 | private static final boolean DEFAULT_COMPATIBLE_SERIALIZATION; |
| 96 | |
| 97 | static { |
| 98 | String issuEnabled = System.getProperty(ISSU_PROPERTY_NAME); |
| 99 | DEFAULT_COMPATIBLE_SERIALIZATION = Strings.isNullOrEmpty(issuEnabled) |
| 100 | ? DEFAULT_ISSU_ENABLED : Boolean.parseBoolean(issuEnabled); |
| 101 | } |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 102 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 103 | private int blockHeadId = INITIAL_ID; |
Jordan Halterman | 0d1f3fa | 2017-05-26 00:14:27 -0700 | [diff] [blame] | 104 | private List<Pair<Class<?>[], Serializer<?>>> types = new ArrayList<>(); |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 105 | private List<RegistrationBlock> blocks = new ArrayList<>(); |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 106 | private boolean registrationRequired = true; |
Jordan Halterman | e1558e8 | 2017-06-13 14:38:20 -0700 | [diff] [blame] | 107 | private boolean compatible = DEFAULT_COMPATIBLE_SERIALIZATION; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 108 | |
| 109 | /** |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 110 | * Builds a {@link KryoNamespace} instance. |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 111 | * |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 112 | * @return KryoNamespace |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 113 | */ |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 114 | public KryoNamespace build() { |
HIGUCHI Yuta | b2d0fd8 | 2016-05-17 20:34:58 -0700 | [diff] [blame] | 115 | return build(NO_NAME); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Builds a {@link KryoNamespace} instance. |
| 120 | * |
| 121 | * @param friendlyName friendly name for the namespace |
| 122 | * @return KryoNamespace |
| 123 | */ |
| 124 | public KryoNamespace build(String friendlyName) { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 125 | if (!types.isEmpty()) { |
| 126 | blocks.add(new RegistrationBlock(this.blockHeadId, types)); |
| 127 | } |
Jordan Halterman | e1558e8 | 2017-06-13 14:38:20 -0700 | [diff] [blame] | 128 | return new KryoNamespace(blocks, registrationRequired, compatible, friendlyName).populate(1); |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Sets the next Kryo registration Id for following register entries. |
| 133 | * |
| 134 | * @param id Kryo registration Id |
| 135 | * @return this |
| 136 | * |
| 137 | * @see Kryo#register(Class, Serializer, int) |
| 138 | */ |
| 139 | public Builder nextId(final int id) { |
| 140 | if (!types.isEmpty()) { |
HIGUCHI Yuta | b49b007 | 2016-02-22 22:50:45 -0800 | [diff] [blame] | 141 | if (id != FLOATING_ID && id < blockHeadId + types.size()) { |
| 142 | |
HIGUCHI Yuta | 163efb5 | 2016-05-18 19:24:19 -0700 | [diff] [blame] | 143 | if (log.isWarnEnabled()) { |
| 144 | log.warn("requested nextId {} could potentially overlap " + |
| 145 | "with existing registrations {}+{} ", |
| 146 | id, blockHeadId, types.size(), new RuntimeException()); |
| 147 | } |
HIGUCHI Yuta | b49b007 | 2016-02-22 22:50:45 -0800 | [diff] [blame] | 148 | } |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 149 | blocks.add(new RegistrationBlock(this.blockHeadId, types)); |
| 150 | types = new ArrayList<>(); |
| 151 | } |
| 152 | this.blockHeadId = id; |
| 153 | return this; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Registers classes to be serialized using Kryo default serializer. |
| 158 | * |
| 159 | * @param expectedTypes list of classes |
| 160 | * @return this |
| 161 | */ |
| 162 | public Builder register(final Class<?>... expectedTypes) { |
| 163 | for (Class<?> clazz : expectedTypes) { |
Jordan Halterman | 0d1f3fa | 2017-05-26 00:14:27 -0700 | [diff] [blame] | 164 | types.add(Pair.of(new Class<?>[]{clazz}, null)); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 165 | } |
| 166 | return this; |
| 167 | } |
| 168 | |
| 169 | /** |
Jordan Halterman | 0d1f3fa | 2017-05-26 00:14:27 -0700 | [diff] [blame] | 170 | * Registers serializer for the given set of classes. |
| 171 | * <p> |
| 172 | * When multiple classes are registered with an explicitly provided serializer, the namespace guarantees |
| 173 | * all instances will be serialized with the same type ID. |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 174 | * |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 175 | * @param classes list of classes to register |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 176 | * @param serializer serializer to use for the class |
| 177 | * @return this |
| 178 | */ |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 179 | public Builder register(Serializer<?> serializer, final Class<?>... classes) { |
Jordan Halterman | 0d1f3fa | 2017-05-26 00:14:27 -0700 | [diff] [blame] | 180 | types.add(Pair.of(classes, checkNotNull(serializer))); |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 181 | return this; |
| 182 | } |
| 183 | |
| 184 | private Builder register(RegistrationBlock block) { |
| 185 | if (block.begin() != FLOATING_ID) { |
| 186 | // flush pending types |
| 187 | nextId(block.begin()); |
| 188 | blocks.add(block); |
| 189 | nextId(block.begin() + block.types().size()); |
| 190 | } else { |
| 191 | // flush pending types |
| 192 | final int addedBlockBegin = blockHeadId + types.size(); |
| 193 | nextId(addedBlockBegin); |
| 194 | blocks.add(new RegistrationBlock(addedBlockBegin, block.types())); |
| 195 | nextId(addedBlockBegin + block.types().size()); |
| 196 | } |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 197 | return this; |
| 198 | } |
| 199 | |
| 200 | /** |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 201 | * Registers all the class registered to given KryoNamespace. |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 202 | * |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 203 | * @param ns KryoNamespace |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 204 | * @return this |
| 205 | */ |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 206 | public Builder register(final KryoNamespace ns) { |
HIGUCHI Yuta | b2d0fd8 | 2016-05-17 20:34:58 -0700 | [diff] [blame] | 207 | |
| 208 | if (blocks.containsAll(ns.registeredBlocks)) { |
| 209 | // Everything was already registered. |
| 210 | log.debug("Ignoring {}, already registered.", ns); |
| 211 | return this; |
| 212 | } |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 213 | for (RegistrationBlock block : ns.registeredBlocks) { |
| 214 | this.register(block); |
| 215 | } |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 216 | return this; |
| 217 | } |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 218 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 219 | /** |
Jordan Halterman | e1558e8 | 2017-06-13 14:38:20 -0700 | [diff] [blame] | 220 | * Sets whether backwards/forwards compatible versioned serialization is enabled. |
| 221 | * <p> |
| 222 | * When compatible serialization is enabled, the {@link CompatibleFieldSerializer} will be set as the |
| 223 | * default serializer for types that do not otherwise explicitly specify a serializer. |
| 224 | * |
| 225 | * @param compatible whether versioned serialization is enabled |
| 226 | * @return this |
| 227 | */ |
| 228 | public Builder setCompatible(boolean compatible) { |
| 229 | this.compatible = compatible; |
| 230 | return this; |
| 231 | } |
| 232 | |
| 233 | /** |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 234 | * Sets the registrationRequired flag. |
| 235 | * |
| 236 | * @param registrationRequired Kryo's registrationRequired flag |
| 237 | * @return this |
| 238 | * |
| 239 | * @see Kryo#setRegistrationRequired(boolean) |
| 240 | */ |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 241 | public Builder setRegistrationRequired(boolean registrationRequired) { |
| 242 | this.registrationRequired = registrationRequired; |
| 243 | return this; |
| 244 | } |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /** |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 248 | * Creates a new {@link KryoNamespace} builder. |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 249 | * |
| 250 | * @return builder |
| 251 | */ |
| 252 | public static Builder newBuilder() { |
| 253 | return new Builder(); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Creates a Kryo instance pool. |
| 258 | * |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 259 | * @param registeredTypes types to register |
Jordan Halterman | e1558e8 | 2017-06-13 14:38:20 -0700 | [diff] [blame] | 260 | * @param registrationRequired whether registration is required |
| 261 | * @param compatible whether compatible serialization is enabled |
HIGUCHI Yuta | b2d0fd8 | 2016-05-17 20:34:58 -0700 | [diff] [blame] | 262 | * @param friendlyName friendly name for the namespace |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 263 | */ |
HIGUCHI Yuta | b2d0fd8 | 2016-05-17 20:34:58 -0700 | [diff] [blame] | 264 | private KryoNamespace(final List<RegistrationBlock> registeredTypes, |
| 265 | boolean registrationRequired, |
Jordan Halterman | e1558e8 | 2017-06-13 14:38:20 -0700 | [diff] [blame] | 266 | boolean compatible, |
HIGUCHI Yuta | b2d0fd8 | 2016-05-17 20:34:58 -0700 | [diff] [blame] | 267 | String friendlyName) { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 268 | this.registeredBlocks = ImmutableList.copyOf(registeredTypes); |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 269 | this.registrationRequired = registrationRequired; |
Jordan Halterman | e1558e8 | 2017-06-13 14:38:20 -0700 | [diff] [blame] | 270 | this.compatible = compatible; |
HIGUCHI Yuta | b2d0fd8 | 2016-05-17 20:34:58 -0700 | [diff] [blame] | 271 | this.friendlyName = checkNotNull(friendlyName); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Populates the Kryo pool. |
| 276 | * |
| 277 | * @param instances to add to the pool |
| 278 | * @return this |
| 279 | */ |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 280 | public KryoNamespace populate(int instances) { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 281 | |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 282 | for (int i = 0; i < instances; ++i) { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 283 | release(create()); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 284 | } |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 285 | return this; |
| 286 | } |
| 287 | |
| 288 | /** |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 289 | * Serializes given object to byte array using Kryo instance in pool. |
| 290 | * <p> |
Yuta HIGUCHI | 3878205 | 2014-11-09 23:51:58 -0800 | [diff] [blame] | 291 | * Note: Serialized bytes must be smaller than {@link #MAX_BUFFER_SIZE}. |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 292 | * |
| 293 | * @param obj Object to serialize |
| 294 | * @return serialized bytes |
| 295 | */ |
| 296 | public byte[] serialize(final Object obj) { |
| 297 | return serialize(obj, DEFAULT_BUFFER_SIZE); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Serializes given object to byte array using Kryo instance in pool. |
| 302 | * |
| 303 | * @param obj Object to serialize |
| 304 | * @param bufferSize maximum size of serialized bytes |
| 305 | * @return serialized bytes |
| 306 | */ |
| 307 | public byte[] serialize(final Object obj, final int bufferSize) { |
Jordan Halterman | ecfca4f | 2017-08-03 21:38:02 -0700 | [diff] [blame] | 308 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(bufferSize); |
| 309 | Output out = new Output(outputStream); |
Yuta HIGUCHI | 86f142f | 2016-07-09 17:44:09 -0700 | [diff] [blame] | 310 | return pool.run(kryo -> { |
| 311 | kryo.writeClassAndObject(out, obj); |
| 312 | out.flush(); |
Jordan Halterman | ecfca4f | 2017-08-03 21:38:02 -0700 | [diff] [blame] | 313 | return outputStream.toByteArray(); |
Yuta HIGUCHI | 86f142f | 2016-07-09 17:44:09 -0700 | [diff] [blame] | 314 | }); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /** |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 318 | * Serializes given object to byte buffer using Kryo instance in pool. |
| 319 | * |
| 320 | * @param obj Object to serialize |
| 321 | * @param buffer to write to |
| 322 | */ |
| 323 | public void serialize(final Object obj, final ByteBuffer buffer) { |
| 324 | ByteBufferOutput out = new ByteBufferOutput(buffer); |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 325 | Kryo kryo = borrow(); |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 326 | try { |
| 327 | kryo.writeClassAndObject(out, obj); |
Yuta HIGUCHI | cac919c | 2014-10-20 22:17:20 -0700 | [diff] [blame] | 328 | out.flush(); |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 329 | } finally { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 330 | release(kryo); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Serializes given object to OutputStream using Kryo instance in pool. |
| 336 | * |
| 337 | * @param obj Object to serialize |
| 338 | * @param stream to write to |
| 339 | */ |
| 340 | public void serialize(final Object obj, final OutputStream stream) { |
| 341 | serialize(obj, stream, DEFAULT_BUFFER_SIZE); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Serializes given object to OutputStream using Kryo instance in pool. |
| 346 | * |
| 347 | * @param obj Object to serialize |
| 348 | * @param stream to write to |
| 349 | * @param bufferSize size of the buffer in front of the stream |
| 350 | */ |
| 351 | public void serialize(final Object obj, final OutputStream stream, final int bufferSize) { |
| 352 | ByteBufferOutput out = new ByteBufferOutput(stream, bufferSize); |
| 353 | Kryo kryo = borrow(); |
| 354 | try { |
| 355 | kryo.writeClassAndObject(out, obj); |
| 356 | out.flush(); |
| 357 | } finally { |
| 358 | release(kryo); |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
| 362 | /** |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 363 | * Deserializes given byte array to Object using Kryo instance in pool. |
| 364 | * |
| 365 | * @param bytes serialized bytes |
| 366 | * @param <T> deserialized Object type |
| 367 | * @return deserialized Object |
| 368 | */ |
| 369 | public <T> T deserialize(final byte[] bytes) { |
Jordan Halterman | ecfca4f | 2017-08-03 21:38:02 -0700 | [diff] [blame] | 370 | Input in = new Input(new ByteArrayInputStream(bytes)); |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 371 | Kryo kryo = borrow(); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 372 | try { |
| 373 | @SuppressWarnings("unchecked") |
| 374 | T obj = (T) kryo.readClassAndObject(in); |
| 375 | return obj; |
| 376 | } finally { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 377 | release(kryo); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 381 | /** |
| 382 | * Deserializes given byte buffer to Object using Kryo instance in pool. |
| 383 | * |
| 384 | * @param buffer input with serialized bytes |
| 385 | * @param <T> deserialized Object type |
| 386 | * @return deserialized Object |
| 387 | */ |
| 388 | public <T> T deserialize(final ByteBuffer buffer) { |
| 389 | ByteBufferInput in = new ByteBufferInput(buffer); |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 390 | Kryo kryo = borrow(); |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 391 | try { |
| 392 | @SuppressWarnings("unchecked") |
| 393 | T obj = (T) kryo.readClassAndObject(in); |
| 394 | return obj; |
| 395 | } finally { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 396 | release(kryo); |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 397 | } |
| 398 | } |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 399 | |
| 400 | /** |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 401 | * Deserializes given InputStream to an Object using Kryo instance in pool. |
| 402 | * |
| 403 | * @param stream input stream |
| 404 | * @param <T> deserialized Object type |
| 405 | * @return deserialized Object |
| 406 | */ |
| 407 | public <T> T deserialize(final InputStream stream) { |
| 408 | return deserialize(stream, DEFAULT_BUFFER_SIZE); |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Deserializes given InputStream to an Object using Kryo instance in pool. |
| 413 | * |
| 414 | * @param stream input stream |
| 415 | * @param <T> deserialized Object type |
| 416 | * @return deserialized Object |
| 417 | * @param bufferSize size of the buffer in front of the stream |
| 418 | */ |
| 419 | public <T> T deserialize(final InputStream stream, final int bufferSize) { |
| 420 | ByteBufferInput in = new ByteBufferInput(stream, bufferSize); |
| 421 | Kryo kryo = borrow(); |
| 422 | try { |
| 423 | @SuppressWarnings("unchecked") |
| 424 | T obj = (T) kryo.readClassAndObject(in); |
| 425 | return obj; |
| 426 | } finally { |
| 427 | release(kryo); |
| 428 | } |
| 429 | } |
| 430 | |
HIGUCHI Yuta | b2d0fd8 | 2016-05-17 20:34:58 -0700 | [diff] [blame] | 431 | private String friendlyName() { |
| 432 | return friendlyName; |
| 433 | } |
| 434 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 435 | /** |
Jonathan Hart | 679d24d | 2016-05-13 13:29:53 -0700 | [diff] [blame] | 436 | * Gets the number of classes registered in this Kryo namespace. |
| 437 | * |
| 438 | * @return size of namespace |
| 439 | */ |
| 440 | public int size() { |
| 441 | return (int) registeredBlocks.stream() |
| 442 | .flatMap(block -> block.types().stream()) |
| 443 | .count(); |
| 444 | } |
| 445 | |
| 446 | /** |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 447 | * Creates a Kryo instance. |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 448 | * |
| 449 | * @return Kryo instance |
| 450 | */ |
Yuta HIGUCHI | 633cf88 | 2014-10-20 09:10:28 -0700 | [diff] [blame] | 451 | @Override |
| 452 | public Kryo create() { |
HIGUCHI Yuta | 0a1f29e | 2016-05-05 15:34:41 -0700 | [diff] [blame] | 453 | log.trace("Creating Kryo instance for {}", this); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 454 | Kryo kryo = new Kryo(); |
| 455 | kryo.setRegistrationRequired(registrationRequired); |
Jordan Halterman | e1558e8 | 2017-06-13 14:38:20 -0700 | [diff] [blame] | 456 | |
| 457 | // If compatible serialization is enabled, override the default serializer. |
| 458 | if (compatible) { |
| 459 | kryo.setDefaultSerializer(CompatibleFieldSerializer::new); |
| 460 | } |
Jonathan Hart | be093f7 | 2016-03-25 11:14:29 -0700 | [diff] [blame] | 461 | |
| 462 | // TODO rethink whether we want to use StdInstantiatorStrategy |
| 463 | kryo.setInstantiatorStrategy( |
| 464 | new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy())); |
| 465 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 466 | for (RegistrationBlock block : registeredBlocks) { |
| 467 | int id = block.begin(); |
| 468 | if (id == FLOATING_ID) { |
| 469 | id = kryo.getNextRegistrationId(); |
| 470 | } |
Jordan Halterman | 0d1f3fa | 2017-05-26 00:14:27 -0700 | [diff] [blame] | 471 | for (Pair<Class<?>[], Serializer<?>> entry : block.types()) { |
HIGUCHI Yuta | 0a1f29e | 2016-05-05 15:34:41 -0700 | [diff] [blame] | 472 | register(kryo, entry.getLeft(), entry.getRight(), id++); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | return kryo; |
| 476 | } |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 477 | |
HIGUCHI Yuta | 0a1f29e | 2016-05-05 15:34:41 -0700 | [diff] [blame] | 478 | /** |
| 479 | * Register {@code type} and {@code serializer} to {@code kryo} instance. |
| 480 | * |
| 481 | * @param kryo Kryo instance |
Jordan Halterman | 0d1f3fa | 2017-05-26 00:14:27 -0700 | [diff] [blame] | 482 | * @param types types to register |
HIGUCHI Yuta | 0a1f29e | 2016-05-05 15:34:41 -0700 | [diff] [blame] | 483 | * @param serializer Specific serializer to register or null to use default. |
| 484 | * @param id type registration id to use |
| 485 | */ |
Jordan Halterman | 0d1f3fa | 2017-05-26 00:14:27 -0700 | [diff] [blame] | 486 | private void register(Kryo kryo, Class<?>[] types, Serializer<?> serializer, int id) { |
HIGUCHI Yuta | 0a1f29e | 2016-05-05 15:34:41 -0700 | [diff] [blame] | 487 | Registration existing = kryo.getRegistration(id); |
| 488 | if (existing != null) { |
Jordan Halterman | 0d1f3fa | 2017-05-26 00:14:27 -0700 | [diff] [blame] | 489 | boolean matches = false; |
| 490 | for (Class<?> type : types) { |
| 491 | if (existing.getType() == type) { |
| 492 | matches = true; |
| 493 | break; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | if (!matches) { |
HIGUCHI Yuta | b2d0fd8 | 2016-05-17 20:34:58 -0700 | [diff] [blame] | 498 | log.error("{}: Failed to register {} as {}, {} was already registered.", |
Jordan Halterman | 0d1f3fa | 2017-05-26 00:14:27 -0700 | [diff] [blame] | 499 | friendlyName(), types, id, existing.getType()); |
HIGUCHI Yuta | 0a1f29e | 2016-05-05 15:34:41 -0700 | [diff] [blame] | 500 | |
| 501 | throw new IllegalStateException(String.format( |
| 502 | "Failed to register %s as %s, %s was already registered.", |
Jordan Halterman | 0d1f3fa | 2017-05-26 00:14:27 -0700 | [diff] [blame] | 503 | Arrays.toString(types), id, existing.getType())); |
HIGUCHI Yuta | 0a1f29e | 2016-05-05 15:34:41 -0700 | [diff] [blame] | 504 | } |
| 505 | // falling through to register call for now. |
| 506 | // Consider skipping, if there's reasonable |
| 507 | // way to compare serializer equivalence. |
| 508 | } |
Jordan Halterman | 0d1f3fa | 2017-05-26 00:14:27 -0700 | [diff] [blame] | 509 | |
| 510 | for (Class<?> type : types) { |
| 511 | Registration r; |
| 512 | if (serializer == null) { |
| 513 | r = kryo.register(type, id); |
| 514 | } else { |
| 515 | r = kryo.register(type, serializer, id); |
| 516 | } |
| 517 | if (r.getId() != id) { |
Jordan Halterman | 05bdb60 | 2017-07-28 17:05:57 -0700 | [diff] [blame] | 518 | log.debug("{}: {} already registered as {}. Skipping {}.", |
Jordan Halterman | 0d1f3fa | 2017-05-26 00:14:27 -0700 | [diff] [blame] | 519 | friendlyName(), r.getType(), r.getId(), id); |
| 520 | } |
| 521 | log.trace("{} registered as {}", r.getType(), r.getId()); |
HIGUCHI Yuta | 0a1f29e | 2016-05-05 15:34:41 -0700 | [diff] [blame] | 522 | } |
HIGUCHI Yuta | 0a1f29e | 2016-05-05 15:34:41 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 525 | @Override |
| 526 | public Kryo borrow() { |
| 527 | return pool.borrow(); |
| 528 | } |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 529 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 530 | @Override |
| 531 | public void release(Kryo kryo) { |
| 532 | pool.release(kryo); |
| 533 | } |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 534 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 535 | @Override |
| 536 | public <T> T run(KryoCallback<T> callback) { |
| 537 | return pool.run(callback); |
| 538 | } |
| 539 | |
| 540 | @Override |
| 541 | public String toString() { |
HIGUCHI Yuta | b2d0fd8 | 2016-05-17 20:34:58 -0700 | [diff] [blame] | 542 | if (friendlyName != NO_NAME) { |
| 543 | return MoreObjects.toStringHelper(getClass()) |
| 544 | .omitNullValues() |
| 545 | .add("friendlyName", friendlyName) |
| 546 | // omit lengthy detail, when there's a name |
| 547 | .toString(); |
| 548 | } |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 549 | return MoreObjects.toStringHelper(getClass()) |
| 550 | .add("registeredBlocks", registeredBlocks) |
| 551 | .toString(); |
| 552 | } |
| 553 | |
| 554 | static final class RegistrationBlock { |
| 555 | private final int begin; |
Jordan Halterman | 0d1f3fa | 2017-05-26 00:14:27 -0700 | [diff] [blame] | 556 | private final ImmutableList<Pair<Class<?>[], Serializer<?>>> types; |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 557 | |
Jordan Halterman | 0d1f3fa | 2017-05-26 00:14:27 -0700 | [diff] [blame] | 558 | public RegistrationBlock(int begin, List<Pair<Class<?>[], Serializer<?>>> types) { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 559 | this.begin = begin; |
| 560 | this.types = ImmutableList.copyOf(types); |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 561 | } |
| 562 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 563 | public int begin() { |
| 564 | return begin; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Yuta HIGUCHI | ede3ae3 | 2018-06-12 16:10:10 -0700 | [diff] [blame] | 567 | // deprecated in 1.14.0 to be made private |
| 568 | @Deprecated |
Jordan Halterman | 0d1f3fa | 2017-05-26 00:14:27 -0700 | [diff] [blame] | 569 | public ImmutableList<Pair<Class<?>[], Serializer<?>>> types() { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 570 | return types; |
| 571 | } |
| 572 | |
| 573 | @Override |
| 574 | public String toString() { |
| 575 | return MoreObjects.toStringHelper(getClass()) |
| 576 | .add("begin", begin) |
| 577 | .add("types", types) |
| 578 | .toString(); |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 579 | } |
HIGUCHI Yuta | b2d0fd8 | 2016-05-17 20:34:58 -0700 | [diff] [blame] | 580 | |
| 581 | @Override |
| 582 | public int hashCode() { |
| 583 | return types.hashCode(); |
| 584 | } |
| 585 | |
| 586 | // Only the registered types are used for equality. |
| 587 | @Override |
| 588 | public boolean equals(Object obj) { |
| 589 | if (this == obj) { |
| 590 | return true; |
| 591 | } |
| 592 | |
| 593 | if (obj instanceof RegistrationBlock) { |
| 594 | RegistrationBlock that = (RegistrationBlock) obj; |
| 595 | return Objects.equals(this.types, that.types); |
| 596 | } |
| 597 | return false; |
| 598 | } |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 599 | } |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 600 | } |