Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

AvroStorBusClientHttp now can negotiate Schemas (lazily) after being …

…constructed
  • Loading branch information...
commit c0d6e5e6baf21463785bdfec285c5178734753ce 1 parent d82f95e
Ting Chen tingchen authored
88 krati-avro/src/main/java/krati/store/bus/client/AvroStoreBusClientHttp.java
... ... @@ -1,5 +1,5 @@
1 1 /*
2   - * Copyright (c) 2010-2012 LinkedIn, Inc
  2 + * Copyright (c) 2011 LinkedIn, Inc
3 3 *
4 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 5 * use this file except in compliance with the License. You may obtain a copy of
@@ -18,6 +18,7 @@
18 18
19 19 import java.net.URL;
20 20
  21 +import krati.io.SerializationException;
21 22 import krati.io.Serializer;
22 23 import krati.store.avro.AvroGenericRecordSerializer;
23 24 import krati.store.avro.protocol.StoreKeys;
@@ -32,7 +33,6 @@
32 33 * @since 10/04, 2011
33 34 */
34 35 public class AvroStoreBusClientHttp<K> extends StoreBusClientHttp<K, GenericRecord> {
35   - protected Schema _schema;
36 36
37 37 public AvroStoreBusClientHttp(URL serverURL, String source, Serializer<K> keySerializer) {
38 38 super(serverURL, source, keySerializer, null);
@@ -41,26 +41,86 @@ public AvroStoreBusClientHttp(URL serverURL, String source, Serializer<K> keySer
41 41 @Override
42 42 protected boolean init() {
43 43 boolean ret = super.init();
44   -
45   - try {
46   - String prop = getProperty(StoreKeys.KRATI_STORE_VALUE_SCHEMA);
47   - _schema = Schema.parse(prop);
48   - _valueSerializer = new AvroGenericRecordSerializer(_schema);
49   - } catch(Exception e) {
50   - ret = false;
51   - }
52   -
  44 + _valueSerializer = new LazyAvroGenericRecordSerializer();
53 45 return ret;
54 46 }
55 47
  48 +
56 49 /**
57 50 * @return the Avro schema of a remote store.
58 51 */
59 52 public final Schema getSchema() {
60   - if(_schema == null) {
61   - init();
  53 + return ((LazyAvroGenericRecordSerializer)_valueSerializer).getSchema();
  54 + }
  55 +
  56 +
  57 +
  58 + /**
  59 + * A {@link Serializer} implementation that will lazily negotiate an avro {@link Schema} with
  60 + * the remote krati store.
  61 + *
  62 + * It will keep negotiating until it succeeds once. After that, it will ALWAYS use the same
  63 + * {@link Schema}.
  64 + * All {@link #serialize(GenericRecord)} and {@link #deserialize(byte[])} calls will throw an
  65 + * {@link IllegalStateException} if a {@link Schema} has not been negotiated yet.
  66 + *
  67 + * @author dbuthay
  68 + *
  69 + */
  70 + private class LazyAvroGenericRecordSerializer implements Serializer<GenericRecord> {
  71 + private AvroGenericRecordSerializer _delegate = null;
  72 + private Schema _schema = null;
  73 +
  74 + @Override
  75 + public GenericRecord deserialize(byte[] bytes) throws SerializationException {
  76 + checkSchema();
  77 + return _delegate.deserialize(bytes);
  78 +
  79 + }
  80 + @Override
  81 + public byte[] serialize(GenericRecord record) throws SerializationException {
  82 + checkSchema();
  83 + return _delegate.serialize(record);
  84 + }
  85 +
  86 + /**
  87 + * Returns the {@link Schema} negotiated with the remote server or {@code null}
  88 + * if negotiation never succeeded.
  89 + *
  90 + * Reasons for negotiation not succeeding include
  91 + * <ul>
  92 + * <li>Network problems</li>
  93 + * <li>Schema String representation retrieved over the network is not parseable</li>
  94 + * <ul>
  95 + *
  96 + * NOTE: This method will NOT try to negotiate a Schema.
  97 + * @return the {@link Schema} negotiated with the remote server or {@code null} if negotiation never succeeded.
  98 + */
  99 + public Schema getSchema() {
  100 + return _schema;
  101 + }
  102 +
  103 +
  104 + /**
  105 + * Check if the {@link Schema} has already been negotiated.
  106 + * If not, try to negotiate and fail if not possible
  107 + *
  108 + * @throws IllegalStateException if there was a problem negotiating the Schema, or
  109 + * if the Schema is invalid.
  110 + */
  111 + private void checkSchema() {
  112 + if (_delegate == null) {
  113 + // try to create a delegate,
  114 + // First we need to negotiate a Schema
  115 + try {
  116 + String prop = getProperty(StoreKeys.KRATI_STORE_VALUE_SCHEMA);
  117 + _schema = Schema.parse(prop);
  118 + _delegate = new AvroGenericRecordSerializer(_schema);
  119 + } catch (Exception e) {
  120 + throw new IllegalStateException("while negotiating Schema: " + e.getMessage(), e);
  121 + }
  122 + }
62 123 }
63 124
64   - return _schema;
65 125 }
66 126 }

0 comments on commit c0d6e5e

Please sign in to comment.
Something went wrong with that request. Please try again.