-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Client] java client support UnAvroBased Schema (#8246)
### Motivation JAVA client support UnAvroBasedSchema ### Modifications - `schema` division to **AvroBasedSchema** and **UnAvroBasedSchema** : AvroBased Schema | UnAvroBased Schema ------------ | ------------- StructSchema | AbstractStructSchema GenericSchemaImpl | AbstractGenericSchema - `GenericSchema` add `of()` method , `GenericSchemaImpl` and `StructSchema` continue to have for backward compatibility and only support AvroBased Schema 。
- Loading branch information
Showing
10 changed files
with
474 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
pulsar-client/src/main/java/org/apache/pulsar/client/impl/schema/AbstractStructSchema.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.client.impl.schema; | ||
|
||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.CacheLoader; | ||
import com.google.common.cache.LoadingCache; | ||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.ByteBufInputStream; | ||
import org.apache.avro.AvroTypeException; | ||
import org.apache.commons.codec.binary.Hex; | ||
import org.apache.commons.lang3.SerializationException; | ||
import org.apache.pulsar.client.api.SchemaSerializationException; | ||
import org.apache.pulsar.client.api.schema.SchemaInfoProvider; | ||
import org.apache.pulsar.client.api.schema.SchemaReader; | ||
import org.apache.pulsar.client.api.schema.SchemaWriter; | ||
import org.apache.pulsar.common.protocol.schema.BytesSchemaVersion; | ||
import org.apache.pulsar.common.schema.SchemaInfo; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* minimal abstract StructSchema | ||
*/ | ||
public abstract class AbstractStructSchema<T> extends AbstractSchema<T> { | ||
|
||
protected static final Logger LOG = LoggerFactory.getLogger(AbstractStructSchema.class); | ||
|
||
protected SchemaInfo schemaInfo; | ||
protected SchemaReader<T> reader; | ||
protected SchemaWriter<T> writer; | ||
protected SchemaInfoProvider schemaInfoProvider; | ||
|
||
LoadingCache<BytesSchemaVersion, SchemaReader<T>> readerCache = CacheBuilder.newBuilder().maximumSize(100000) | ||
.expireAfterAccess(30, TimeUnit.MINUTES).build(new CacheLoader<BytesSchemaVersion, SchemaReader<T>>() { | ||
@Override | ||
public SchemaReader<T> load(BytesSchemaVersion schemaVersion) { | ||
return loadReader(schemaVersion); | ||
} | ||
}); | ||
|
||
public AbstractStructSchema(SchemaInfo schemaInfo){ | ||
this.schemaInfo = schemaInfo; | ||
} | ||
|
||
|
||
@Override | ||
public byte[] encode(T message) { | ||
return writer.write(message); | ||
} | ||
|
||
@Override | ||
public T decode(byte[] bytes) { | ||
return reader.read(bytes); | ||
} | ||
|
||
@Override | ||
public T decode(byte[] bytes, byte[] schemaVersion) { | ||
try { | ||
return schemaVersion == null ? decode(bytes) : | ||
readerCache.get(BytesSchemaVersion.of(schemaVersion)).read(bytes); | ||
} catch (ExecutionException | AvroTypeException e) { | ||
if (e instanceof AvroTypeException) { | ||
throw new SchemaSerializationException(e); | ||
} | ||
LOG.error("Can't get generic schema for topic {} schema version {}", | ||
schemaInfoProvider.getTopicName(), Hex.encodeHexString(schemaVersion), e); | ||
throw new RuntimeException("Can't get generic schema for topic " + schemaInfoProvider.getTopicName()); | ||
} | ||
} | ||
|
||
@Override | ||
public T decode(ByteBuf byteBuf) { | ||
return reader.read(new ByteBufInputStream(byteBuf)); | ||
} | ||
|
||
@Override | ||
public T decode(ByteBuf byteBuf, byte[] schemaVersion) { | ||
try { | ||
return schemaVersion == null ? decode(byteBuf) : | ||
readerCache.get(BytesSchemaVersion.of(schemaVersion)).read(new ByteBufInputStream(byteBuf)); | ||
} catch (ExecutionException e) { | ||
LOG.error("Can't get generic schema for topic {} schema version {}", | ||
schemaInfoProvider.getTopicName(), Hex.encodeHexString(schemaVersion), e); | ||
throw new RuntimeException("Can't get generic schema for topic " + schemaInfoProvider.getTopicName()); | ||
} | ||
} | ||
|
||
@Override | ||
public SchemaInfo getSchemaInfo() { | ||
return this.schemaInfo; | ||
} | ||
|
||
@Override | ||
public void setSchemaInfoProvider(SchemaInfoProvider schemaInfoProvider) { | ||
this.schemaInfoProvider = schemaInfoProvider; | ||
} | ||
|
||
/** | ||
* Load the schema reader for reading messages encoded by the given schema version. | ||
* | ||
* @param schemaVersion the provided schema version | ||
* @return the schema reader for decoding messages encoded by the provided schema version. | ||
*/ | ||
protected abstract SchemaReader<T> loadReader(BytesSchemaVersion schemaVersion); | ||
|
||
/** | ||
* TODO: think about how to make this async | ||
*/ | ||
protected SchemaInfo getSchemaInfoByVersion(byte[] schemaVersion) { | ||
try { | ||
return schemaInfoProvider.getSchemaByVersion(schemaVersion).get(); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new SerializationException( | ||
"Interrupted at fetching schema info for " + SchemaUtils.getStringSchemaVersion(schemaVersion), | ||
e | ||
); | ||
} catch (ExecutionException e) { | ||
throw new SerializationException( | ||
"Failed at fetching schema info for " + SchemaUtils.getStringSchemaVersion(schemaVersion), | ||
e.getCause() | ||
); | ||
} | ||
} | ||
|
||
protected void setWriter(SchemaWriter<T> writer) { | ||
this.writer = writer; | ||
} | ||
|
||
protected void setReader(SchemaReader<T> reader) { | ||
this.reader = reader; | ||
} | ||
|
||
protected SchemaReader<T> getReader() { | ||
return reader; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.