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