avro_turf 0.8.1 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +36 -0
- data/.github/workflows/ruby.yml +20 -0
- data/CHANGELOG.md +15 -0
- data/Gemfile +0 -3
- data/README.md +20 -0
- data/avro_turf.gemspec +3 -2
- data/lib/avro_turf.rb +10 -1
- data/lib/avro_turf/cached_confluent_schema_registry.rb +18 -6
- data/lib/avro_turf/disk_cache.rb +83 -0
- data/lib/avro_turf/in_memory_cache.rb +38 -0
- data/lib/avro_turf/messaging.rb +70 -7
- data/lib/avro_turf/schema_store.rb +35 -20
- data/lib/avro_turf/test/fake_confluent_schema_registry_server.rb +15 -3
- data/lib/avro_turf/version.rb +1 -1
- data/spec/cached_confluent_schema_registry_spec.rb +24 -2
- data/spec/disk_cached_confluent_schema_registry_spec.rb +159 -0
- data/spec/messaging_spec.rb +198 -16
- data/spec/schema_store_spec.rb +36 -0
- data/spec/spec_helper.rb +8 -0
- data/spec/support/confluent_schema_registry_context.rb +8 -5
- data/spec/test/fake_confluent_schema_registry_server_spec.rb +40 -0
- metadata +28 -7
- data/circle.yml +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 622818bec3f2c9af6a801fe428b373c96ad38596bef90e6f24d86c855084a1a9
|
4
|
+
data.tar.gz: 386053febb649eb457fd0fca3685556e074bee28994be511944da5d7b75b594b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 35663cd1404da25e60c03bdfcca2dc20fd1af8fb5eb046bad57fb079f4e50901120e15ae3e6f9d275b068202fd113613b49b9e9237ca2705de3f1fd0e34c304a
|
7
|
+
data.tar.gz: a37f52541946c02c4858c73e974f78e3e88702fe31292807dd46760e9e2a4dabad8287b136df0ff95ba6912cbb15760881e9574f2837a3124242a00e9d6fac75
|
@@ -0,0 +1,36 @@
|
|
1
|
+
version: 2
|
2
|
+
jobs:
|
3
|
+
build:
|
4
|
+
environment:
|
5
|
+
CIRCLE_ARTIFACTS: /tmp/circleci-artifacts
|
6
|
+
CIRCLE_TEST_REPORTS: /tmp/circleci-test-results
|
7
|
+
docker:
|
8
|
+
- image: circleci/ruby:2.6.2
|
9
|
+
steps:
|
10
|
+
- checkout
|
11
|
+
- run: mkdir -p $CIRCLE_ARTIFACTS $CIRCLE_TEST_REPORTS
|
12
|
+
- restore_cache:
|
13
|
+
keys:
|
14
|
+
# This branch if available
|
15
|
+
- v1-dep-{{ .Branch }}-
|
16
|
+
# Default branch if not
|
17
|
+
- v1-dep-master-
|
18
|
+
# Any branch if there are none on the default branch - this should be unnecessary if you have your default branch configured correctly
|
19
|
+
- v1-dep-
|
20
|
+
- run: gem install bundler --no-document
|
21
|
+
- run: 'bundle check --path=vendor/bundle || bundle install --path=vendor/bundle --jobs=4 --retry=3'
|
22
|
+
# Save dependency cache
|
23
|
+
- save_cache:
|
24
|
+
key: v1-dep-{{ .Branch }}-{{ epoch }}
|
25
|
+
paths:
|
26
|
+
- vendor/bundle
|
27
|
+
- ~/.bundle
|
28
|
+
- run: mkdir -p $CIRCLE_TEST_REPORTS/rspec
|
29
|
+
- run:
|
30
|
+
command: bundle exec rspec --color --require spec_helper --format progress
|
31
|
+
- store_test_results:
|
32
|
+
path: /tmp/circleci-test-results
|
33
|
+
- store_artifacts:
|
34
|
+
path: /tmp/circleci-artifacts
|
35
|
+
- store_artifacts:
|
36
|
+
path: /tmp/circleci-test-results
|
@@ -0,0 +1,20 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on: [push]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
|
8
|
+
runs-on: ubuntu-latest
|
9
|
+
|
10
|
+
steps:
|
11
|
+
- uses: actions/checkout@v1
|
12
|
+
- name: Set up Ruby 2.6
|
13
|
+
uses: actions/setup-ruby@v1
|
14
|
+
with:
|
15
|
+
ruby-version: 2.6.x
|
16
|
+
- name: Build and test with RSpec
|
17
|
+
run: |
|
18
|
+
gem install bundler
|
19
|
+
bundle install --jobs 4 --retry 3
|
20
|
+
bundle exec rspec
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,20 @@
|
|
1
1
|
# avro_turf
|
2
2
|
|
3
|
+
## Unreleased
|
4
|
+
|
5
|
+
## v0.10.0
|
6
|
+
|
7
|
+
- Add more disk caching (#103)
|
8
|
+
- Include schema information when decoding (#100, #101, #104)
|
9
|
+
|
10
|
+
## v0.9.0
|
11
|
+
|
12
|
+
- Compatibility with Avro v1.9.0 (#94)
|
13
|
+
- Disable the auto registeration of schema (#95)
|
14
|
+
- abstracted caching from CachedConfluentSchemaRegistry (#74)
|
15
|
+
- Load avro-patches if installed to silence deprecation errors (#85)
|
16
|
+
- Make schema store to be thread safe (#92)
|
17
|
+
|
3
18
|
## v0.8.1
|
4
19
|
|
5
20
|
- Allow accessing schema store from outside AvroTurf (#68).
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -124,9 +124,29 @@ avro = AvroTurf::Messaging.new(registry_url: "http://my-registry:8081/")
|
|
124
124
|
# time a schema is used.
|
125
125
|
data = avro.encode({ "title" => "hello, world" }, schema_name: "greeting")
|
126
126
|
|
127
|
+
# If you don't want to automatically register new schemas, you can pass explicitly
|
128
|
+
# subject and version to specify which schema should be used for encoding.
|
129
|
+
# It will fetch that schema from the registry and cache it. Subsequent instances
|
130
|
+
# of the same schema version will be served by the cache.
|
131
|
+
data = avro.encode({ "title" => "hello, world" }, subject: 'greeting', version: 1)
|
132
|
+
|
133
|
+
# You can also pass explicitly schema_id to specify which schema
|
134
|
+
# should be used for encoding.
|
135
|
+
# It will fetch that schema from the registry and cache it. Subsequent instances
|
136
|
+
# of the same schema version will be served by the cache.
|
137
|
+
data = avro.encode({ "title" => "hello, world" }, schema_id: 2)
|
138
|
+
|
127
139
|
# When decoding, the schema will be fetched from the registry and cached. Subsequent
|
128
140
|
# instances of the same schema id will be served by the cache.
|
129
141
|
avro.decode(data) #=> { "title" => "hello, world" }
|
142
|
+
|
143
|
+
# If you want to get decoded message as well as the schema used to encode the message,
|
144
|
+
# you can use `#decode_message` method.
|
145
|
+
result = avro.decode_message(data)
|
146
|
+
result.message #=> { "title" => "hello, world" }
|
147
|
+
result.schema_id #=> 3
|
148
|
+
result.writer_schema #=> #<Avro::Schema: ...>
|
149
|
+
result.reader_schema #=> nil
|
130
150
|
```
|
131
151
|
|
132
152
|
### Confluent Schema Registry Client
|
data/avro_turf.gemspec
CHANGED
@@ -17,16 +17,17 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
|
-
spec.add_dependency "avro", ">= 1.7.7", "< 1.
|
20
|
+
spec.add_dependency "avro", ">= 1.7.7", "< 1.10"
|
21
21
|
spec.add_dependency "excon", "~> 0.45"
|
22
22
|
|
23
|
-
spec.add_development_dependency "bundler", "~>
|
23
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
24
24
|
spec.add_development_dependency "rake", "~> 10.0"
|
25
25
|
spec.add_development_dependency "rspec", "~> 3.2.0"
|
26
26
|
spec.add_development_dependency "fakefs", "~> 0.6.7"
|
27
27
|
spec.add_development_dependency "webmock"
|
28
28
|
spec.add_development_dependency "sinatra"
|
29
29
|
spec.add_development_dependency "json_spec"
|
30
|
+
spec.add_development_dependency "rack-test"
|
30
31
|
|
31
32
|
spec.post_install_message = %{
|
32
33
|
avro_turf v0.8.0 deprecates the names AvroTurf::SchemaRegistry,
|
data/lib/avro_turf.rb
CHANGED
@@ -1,9 +1,18 @@
|
|
1
|
+
begin
|
2
|
+
require 'avro-patches'
|
3
|
+
rescue LoadError
|
4
|
+
false
|
5
|
+
end
|
1
6
|
require 'avro_turf/version'
|
2
7
|
require 'avro'
|
3
8
|
require 'json'
|
4
9
|
require 'avro_turf/schema_store'
|
5
10
|
require 'avro_turf/core_ext'
|
6
|
-
|
11
|
+
|
12
|
+
# check for something that indicates Avro v1.9.0 or later
|
13
|
+
unless defined?(::Avro::LogicalTypes)
|
14
|
+
require 'avro_turf/schema_to_avro_patch'
|
15
|
+
end
|
7
16
|
|
8
17
|
class AvroTurf
|
9
18
|
class Error < StandardError; end
|
@@ -1,16 +1,23 @@
|
|
1
1
|
require 'avro_turf/confluent_schema_registry'
|
2
|
+
require 'avro_turf/in_memory_cache'
|
3
|
+
require 'avro_turf/disk_cache'
|
2
4
|
|
3
5
|
# Caches registrations and lookups to the schema registry in memory.
|
4
6
|
class AvroTurf::CachedConfluentSchemaRegistry
|
5
7
|
|
6
|
-
|
8
|
+
# Instantiate a new CachedConfluentSchemaRegistry instance with the given configuration.
|
9
|
+
# By default, uses a provided InMemoryCache to prevent repeated calls to the upstream registry.
|
10
|
+
#
|
11
|
+
# upstream - The upstream schema registry object that fully responds to all methods in the
|
12
|
+
# AvroTurf::ConfluentSchemaRegistry interface.
|
13
|
+
# cache - Optional user provided Cache object that responds to all methods in the AvroTurf::InMemoryCache interface.
|
14
|
+
def initialize(upstream, cache: nil)
|
7
15
|
@upstream = upstream
|
8
|
-
@
|
9
|
-
@ids_by_schema = {}
|
16
|
+
@cache = cache || AvroTurf::InMemoryCache.new()
|
10
17
|
end
|
11
18
|
|
12
19
|
# Delegate the following methods to the upstream
|
13
|
-
%i(subjects subject_versions
|
20
|
+
%i(subjects subject_versions check compatible?
|
14
21
|
global_config update_global_config subject_config update_subject_config).each do |name|
|
15
22
|
define_method(name) do |*args|
|
16
23
|
instance_variable_get(:@upstream).send(name, *args)
|
@@ -18,10 +25,15 @@ class AvroTurf::CachedConfluentSchemaRegistry
|
|
18
25
|
end
|
19
26
|
|
20
27
|
def fetch(id)
|
21
|
-
@
|
28
|
+
@cache.lookup_by_id(id) || @cache.store_by_id(id, @upstream.fetch(id))
|
22
29
|
end
|
23
30
|
|
24
31
|
def register(subject, schema)
|
25
|
-
@
|
32
|
+
@cache.lookup_by_schema(subject, schema) || @cache.store_by_schema(subject, schema, @upstream.register(subject, schema))
|
33
|
+
end
|
34
|
+
|
35
|
+
def subject_version(subject, version = 'latest')
|
36
|
+
@cache.lookup_by_version(subject, version) ||
|
37
|
+
@cache.store_by_version(subject, version, @upstream.subject_version(subject, version))
|
26
38
|
end
|
27
39
|
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# A cache for the CachedConfluentSchemaRegistry.
|
2
|
+
# Extends the InMemoryCache to provide a write-thru to disk for persistent cache.
|
3
|
+
class AvroTurf::DiskCache < AvroTurf::InMemoryCache
|
4
|
+
|
5
|
+
def initialize(disk_path)
|
6
|
+
super()
|
7
|
+
|
8
|
+
# load the write-thru cache on startup, if it exists
|
9
|
+
@schemas_by_id_path = File.join(disk_path, 'schemas_by_id.json')
|
10
|
+
@schemas_by_id = JSON.parse(File.read(@schemas_by_id_path)) if File.exist?(@schemas_by_id_path)
|
11
|
+
|
12
|
+
@ids_by_schema_path = File.join(disk_path, 'ids_by_schema.json')
|
13
|
+
@ids_by_schema = JSON.parse(File.read(@ids_by_schema_path)) if File.exist?(@ids_by_schema_path)
|
14
|
+
|
15
|
+
@schemas_by_subject_version_path = File.join(disk_path, 'schemas_by_subject_version.json')
|
16
|
+
@schemas_by_subject_version = {}
|
17
|
+
end
|
18
|
+
|
19
|
+
# override
|
20
|
+
# the write-thru cache (json) does not store keys in numeric format
|
21
|
+
# so, convert id to a string for caching purposes
|
22
|
+
def lookup_by_id(id)
|
23
|
+
super(id.to_s)
|
24
|
+
end
|
25
|
+
|
26
|
+
# override to include write-thru cache after storing result from upstream
|
27
|
+
def store_by_id(id, schema)
|
28
|
+
# must return the value from storing the result (i.e. do not return result from file write)
|
29
|
+
value = super(id.to_s, schema)
|
30
|
+
File.write(@schemas_by_id_path, JSON.pretty_generate(@schemas_by_id))
|
31
|
+
return value
|
32
|
+
end
|
33
|
+
|
34
|
+
# override to include write-thru cache after storing result from upstream
|
35
|
+
def store_by_schema(subject, schema, id)
|
36
|
+
# must return the value from storing the result (i.e. do not return result from file write)
|
37
|
+
value = super
|
38
|
+
File.write(@ids_by_schema_path, JSON.pretty_generate(@ids_by_schema))
|
39
|
+
return value
|
40
|
+
end
|
41
|
+
|
42
|
+
# checks instance var (in-memory cache) for schema
|
43
|
+
# checks disk cache if in-memory cache doesn't exists
|
44
|
+
# if file exists but no in-memory cache, read from file and sync in-memory cache
|
45
|
+
# finally, if file doesn't exist return nil
|
46
|
+
def lookup_by_version(subject, version)
|
47
|
+
key = "#{subject}#{version}"
|
48
|
+
schema = @schemas_by_subject_version[key]
|
49
|
+
|
50
|
+
return schema unless schema.nil?
|
51
|
+
|
52
|
+
hash = JSON.parse(File.read(@schemas_by_subject_version_path)) if File.exist?(@schemas_by_subject_version_path)
|
53
|
+
if hash
|
54
|
+
@schemas_by_subject_version = hash
|
55
|
+
@schemas_by_subject_version[key]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# check if file exists and parse json into a hash
|
60
|
+
# if file exists take json and overwite/insert schema at key
|
61
|
+
# if file doesn't exist create new hash
|
62
|
+
# write the new/updated hash to file
|
63
|
+
# update instance var (in memory-cache) to match
|
64
|
+
def store_by_version(subject, version, schema)
|
65
|
+
key = "#{subject}#{version}"
|
66
|
+
hash = JSON.parse(File.read(@schemas_by_subject_version_path)) if File.exist?(@schemas_by_subject_version_path)
|
67
|
+
hash = if hash
|
68
|
+
hash[key] = schema
|
69
|
+
hash
|
70
|
+
else
|
71
|
+
{key => schema}
|
72
|
+
end
|
73
|
+
|
74
|
+
write_to_disk_cache(@schemas_by_subject_version_path, hash)
|
75
|
+
|
76
|
+
@schemas_by_subject_version = hash
|
77
|
+
@schemas_by_subject_version[key]
|
78
|
+
end
|
79
|
+
|
80
|
+
private def write_to_disk_cache(path, hash)
|
81
|
+
File.write(path, JSON.pretty_generate(hash))
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# A cache for the CachedConfluentSchemaRegistry.
|
2
|
+
# Simply stores the schemas and ids in in-memory hashes.
|
3
|
+
class AvroTurf::InMemoryCache
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@schemas_by_id = {}
|
7
|
+
@ids_by_schema = {}
|
8
|
+
@schema_by_subject_version = {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def lookup_by_id(id)
|
12
|
+
@schemas_by_id[id]
|
13
|
+
end
|
14
|
+
|
15
|
+
def store_by_id(id, schema)
|
16
|
+
@schemas_by_id[id] = schema
|
17
|
+
end
|
18
|
+
|
19
|
+
def lookup_by_schema(subject, schema)
|
20
|
+
key = subject + schema.to_s
|
21
|
+
@ids_by_schema[key]
|
22
|
+
end
|
23
|
+
|
24
|
+
def store_by_schema(subject, schema, id)
|
25
|
+
key = subject + schema.to_s
|
26
|
+
@ids_by_schema[key] = id
|
27
|
+
end
|
28
|
+
|
29
|
+
def lookup_by_version(subject, version)
|
30
|
+
key = "#{subject}#{version}"
|
31
|
+
@schema_by_subject_version[key]
|
32
|
+
end
|
33
|
+
|
34
|
+
def store_by_version(subject, version, schema)
|
35
|
+
key = "#{subject}#{version}"
|
36
|
+
@schema_by_subject_version[key] = schema
|
37
|
+
end
|
38
|
+
end
|
data/lib/avro_turf/messaging.rb
CHANGED
@@ -21,6 +21,8 @@ class AvroTurf
|
|
21
21
|
# 1: https://github.com/confluentinc/schema-registry
|
22
22
|
class Messaging
|
23
23
|
MAGIC_BYTE = [0].pack("C").freeze
|
24
|
+
DecodedMessage = Struct.new(:schema_id, :writer_schema, :reader_schema, :message)
|
25
|
+
private_constant(:DecodedMessage)
|
24
26
|
|
25
27
|
# Instantiate a new Messaging instance with the given configuration.
|
26
28
|
#
|
@@ -46,14 +48,24 @@ class AvroTurf
|
|
46
48
|
# schema_name - The String name of the schema that should be used to encode
|
47
49
|
# the data.
|
48
50
|
# namespace - The namespace of the schema (optional).
|
51
|
+
# subject - The subject name the schema should be registered under in
|
52
|
+
# the schema registry (optional).
|
53
|
+
# version - The integer version of the schema that should be used to decode
|
54
|
+
# the data. Must match the schema used when encoding (optional).
|
55
|
+
# schema_id - The integer id of the schema that should be used to encode
|
56
|
+
# the data.
|
49
57
|
#
|
50
58
|
# Returns the encoded data as a String.
|
51
|
-
def encode(message, schema_name: nil, namespace: @namespace, subject: nil)
|
52
|
-
schema =
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
59
|
+
def encode(message, schema_name: nil, namespace: @namespace, subject: nil, version: nil, schema_id: nil)
|
60
|
+
schema_id, schema = if schema_id
|
61
|
+
fetch_schema_by_id(schema_id)
|
62
|
+
elsif subject && version
|
63
|
+
fetch_schema(subject, version)
|
64
|
+
elsif schema_name
|
65
|
+
register_schema(subject, schema_name, namespace)
|
66
|
+
else
|
67
|
+
raise ArgumentError.new('Neither schema_name nor schema_id nor subject + version provided to determine the schema.')
|
68
|
+
end
|
57
69
|
|
58
70
|
stream = StringIO.new
|
59
71
|
writer = Avro::IO::DatumWriter.new(schema)
|
@@ -69,6 +81,12 @@ class AvroTurf
|
|
69
81
|
writer.write(message, encoder)
|
70
82
|
|
71
83
|
stream.string
|
84
|
+
rescue Excon::Error::NotFound
|
85
|
+
if schema_id
|
86
|
+
raise SchemaNotFoundError.new("Schema with id: #{schema_id} is not found on registry")
|
87
|
+
else
|
88
|
+
raise SchemaNotFoundError.new("Schema with subject: `#{subject}` version: `#{version}` is not found on registry")
|
89
|
+
end
|
72
90
|
end
|
73
91
|
|
74
92
|
# Decodes data into the original message.
|
@@ -80,6 +98,20 @@ class AvroTurf
|
|
80
98
|
#
|
81
99
|
# Returns the decoded message.
|
82
100
|
def decode(data, schema_name: nil, namespace: @namespace)
|
101
|
+
decode_message(data, schema_name: schema_name, namespace: namespace).message
|
102
|
+
end
|
103
|
+
|
104
|
+
# Decodes data into the original message.
|
105
|
+
#
|
106
|
+
# data - A String containing encoded data.
|
107
|
+
# schema_name - The String name of the schema that should be used to decode
|
108
|
+
# the data. Must match the schema used when encoding (optional).
|
109
|
+
# namespace - The namespace of the schema (optional).
|
110
|
+
#
|
111
|
+
# Returns Struct with the next attributes:
|
112
|
+
# schema_id - The integer id of schema used to encode the message
|
113
|
+
# message - The decoded message
|
114
|
+
def decode_message(data, schema_name: nil, namespace: @namespace)
|
83
115
|
readers_schema = schema_name && @schema_store.find(schema_name, namespace)
|
84
116
|
stream = StringIO.new(data)
|
85
117
|
decoder = Avro::IO::BinaryDecoder.new(stream)
|
@@ -100,7 +132,38 @@ class AvroTurf
|
|
100
132
|
end
|
101
133
|
|
102
134
|
reader = Avro::IO::DatumReader.new(writers_schema, readers_schema)
|
103
|
-
reader.read(decoder)
|
135
|
+
message = reader.read(decoder)
|
136
|
+
|
137
|
+
DecodedMessage.new(schema_id, writers_schema, readers_schema, message)
|
138
|
+
rescue Excon::Error::NotFound
|
139
|
+
raise SchemaNotFoundError.new("Schema with id: #{schema_id} is not found on registry")
|
140
|
+
end
|
141
|
+
|
142
|
+
private
|
143
|
+
|
144
|
+
# Providing subject and version to determine the schema,
|
145
|
+
# which skips the auto registeration of schema on the schema registry.
|
146
|
+
# Fetch the schema from registry with the provided subject name and version.
|
147
|
+
def fetch_schema(subject, version)
|
148
|
+
schema_data = @registry.subject_version(subject, version)
|
149
|
+
schema_id = schema_data.fetch('id')
|
150
|
+
schema = Avro::Schema.parse(schema_data.fetch('schema'))
|
151
|
+
[schema_id, schema]
|
152
|
+
end
|
153
|
+
|
154
|
+
# Fetch the schema from registry with the provided schema_id.
|
155
|
+
def fetch_schema_by_id(schema_id)
|
156
|
+
schema_json = @registry.fetch(schema_id)
|
157
|
+
schema = Avro::Schema.parse(schema_json)
|
158
|
+
[schema_id, schema]
|
159
|
+
end
|
160
|
+
|
161
|
+
# Schemas are registered under the full name of the top level Avro record
|
162
|
+
# type, or `subject` if it's provided.
|
163
|
+
def register_schema(subject, schema_name, namespace)
|
164
|
+
schema = @schema_store.find(schema_name, namespace)
|
165
|
+
schema_id = @registry.register(subject || schema.fullname, schema)
|
166
|
+
[schema_id, schema]
|
104
167
|
end
|
105
168
|
end
|
106
169
|
end
|
@@ -3,6 +3,7 @@ class AvroTurf::SchemaStore
|
|
3
3
|
def initialize(path: nil)
|
4
4
|
@path = path or raise "Please specify a schema path"
|
5
5
|
@schemas = Hash.new
|
6
|
+
@mutex = Mutex.new
|
6
7
|
end
|
7
8
|
|
8
9
|
# Resolves and returns a schema.
|
@@ -12,9 +13,40 @@ class AvroTurf::SchemaStore
|
|
12
13
|
# Returns an Avro::Schema.
|
13
14
|
def find(name, namespace = nil)
|
14
15
|
fullname = Avro::Name.make_fullname(name, namespace)
|
15
|
-
|
16
|
+
# Optimistic non-blocking read from @schemas
|
17
|
+
# No sense to lock the resource when all the schemas already loaded
|
16
18
|
return @schemas[fullname] if @schemas.key?(fullname)
|
17
19
|
|
20
|
+
# Pessimistic blocking write to @schemas
|
21
|
+
@mutex.synchronize do
|
22
|
+
# Still need to check is the schema already loaded
|
23
|
+
return @schemas[fullname] if @schemas.key?(fullname)
|
24
|
+
|
25
|
+
load_schema!(fullname, namespace)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Loads all schema definition files in the `schemas_dir`.
|
30
|
+
def load_schemas!
|
31
|
+
pattern = [@path, "**", "*.avsc"].join("/")
|
32
|
+
|
33
|
+
Dir.glob(pattern) do |schema_path|
|
34
|
+
# Remove the path prefix.
|
35
|
+
schema_path.sub!(/^\/?#{@path}\//, "")
|
36
|
+
|
37
|
+
# Replace `/` with `.` and chop off the file extension.
|
38
|
+
schema_name = File.basename(schema_path.tr("/", "."), ".avsc")
|
39
|
+
|
40
|
+
# Load and cache the schema.
|
41
|
+
find(schema_name)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
# Loads single schema
|
48
|
+
# Such method is not thread-safe, do not call it of from mutex synchronization routine
|
49
|
+
def load_schema!(fullname, namespace = nil)
|
18
50
|
*namespace, schema_name = fullname.split(".")
|
19
51
|
schema_path = File.join(@path, *namespace, schema_name + ".avsc")
|
20
52
|
schema_json = JSON.parse(File.read(schema_path))
|
@@ -29,32 +61,15 @@ class AvroTurf::SchemaStore
|
|
29
61
|
# This is a hack in order to figure out exactly which type was missing. The
|
30
62
|
# Avro gem ought to provide this data directly.
|
31
63
|
if e.to_s =~ /"([\w\.]+)" is not a schema we know about/
|
32
|
-
|
64
|
+
load_schema!($1)
|
33
65
|
|
34
66
|
# Re-resolve the original schema now that the dependency has been resolved.
|
35
67
|
@schemas.delete(fullname)
|
36
|
-
|
68
|
+
load_schema!(fullname)
|
37
69
|
else
|
38
70
|
raise
|
39
71
|
end
|
40
72
|
rescue Errno::ENOENT, Errno::ENAMETOOLONG
|
41
73
|
raise AvroTurf::SchemaNotFoundError, "could not find Avro schema at `#{schema_path}'"
|
42
74
|
end
|
43
|
-
|
44
|
-
# Loads all schema definition files in the `schemas_dir`.
|
45
|
-
def load_schemas!
|
46
|
-
pattern = [@path, "**", "*.avsc"].join("/")
|
47
|
-
|
48
|
-
Dir.glob(pattern) do |schema_path|
|
49
|
-
# Remove the path prefix.
|
50
|
-
schema_path.sub!(/^\/?#{@path}\//, "")
|
51
|
-
|
52
|
-
# Replace `/` with `.` and chop off the file extension.
|
53
|
-
schema_name = File.basename(schema_path.tr("/", "."), ".avsc")
|
54
|
-
|
55
|
-
# Load and cache the schema.
|
56
|
-
find(schema_name)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
75
|
end
|
@@ -34,10 +34,21 @@ class FakeConfluentSchemaRegistryServer < Sinatra::Base
|
|
34
34
|
end
|
35
35
|
|
36
36
|
post "/subjects/:subject/versions" do
|
37
|
-
|
37
|
+
schema = parse_schema
|
38
|
+
ids_for_subject = SUBJECTS[params[:subject]]
|
39
|
+
|
40
|
+
schemas_for_subject =
|
41
|
+
SCHEMAS.select
|
42
|
+
.with_index { |_, i| ids_for_subject.include?(i) }
|
43
|
+
|
44
|
+
if schemas_for_subject.include?(schema)
|
45
|
+
schema_id = SCHEMAS.index(schema)
|
46
|
+
else
|
47
|
+
SCHEMAS << schema
|
48
|
+
schema_id = SCHEMAS.size - 1
|
49
|
+
SUBJECTS[params[:subject]] = SUBJECTS[params[:subject]] << schema_id
|
50
|
+
end
|
38
51
|
|
39
|
-
schema_id = SCHEMAS.size - 1
|
40
|
-
SUBJECTS[params[:subject]] = SUBJECTS[params[:subject]] << schema_id
|
41
52
|
{ id: schema_id }.to_json
|
42
53
|
end
|
43
54
|
|
@@ -73,6 +84,7 @@ class FakeConfluentSchemaRegistryServer < Sinatra::Base
|
|
73
84
|
{
|
74
85
|
name: params[:subject],
|
75
86
|
version: schema_ids.index(schema_id) + 1,
|
87
|
+
id: schema_id,
|
76
88
|
schema: schema
|
77
89
|
}.to_json
|
78
90
|
end
|
data/lib/avro_turf/version.rb
CHANGED
@@ -16,8 +16,9 @@ describe AvroTurf::CachedConfluentSchemaRegistry do
|
|
16
16
|
|
17
17
|
describe "#fetch" do
|
18
18
|
it "caches the result of fetch" do
|
19
|
+
# multiple calls return same result, with only one upstream call
|
19
20
|
allow(upstream).to receive(:fetch).with(id).and_return(schema)
|
20
|
-
registry.fetch(id)
|
21
|
+
expect(registry.fetch(id)).to eq(schema)
|
21
22
|
expect(registry.fetch(id)).to eq(schema)
|
22
23
|
expect(upstream).to have_received(:fetch).exactly(1).times
|
23
24
|
end
|
@@ -27,13 +28,34 @@ describe AvroTurf::CachedConfluentSchemaRegistry do
|
|
27
28
|
let(:subject_name) { "a_subject" }
|
28
29
|
|
29
30
|
it "caches the result of register" do
|
31
|
+
# multiple calls return same result, with only one upstream call
|
30
32
|
allow(upstream).to receive(:register).with(subject_name, schema).and_return(id)
|
31
|
-
registry.register(subject_name, schema)
|
33
|
+
expect(registry.register(subject_name, schema)).to eq(id)
|
32
34
|
expect(registry.register(subject_name, schema)).to eq(id)
|
33
35
|
expect(upstream).to have_received(:register).exactly(1).times
|
34
36
|
end
|
35
37
|
end
|
36
38
|
|
39
|
+
describe '#subject_version' do
|
40
|
+
let(:subject_name) { 'a_subject' }
|
41
|
+
let(:version) { 1 }
|
42
|
+
let(:schema_with_meta) do
|
43
|
+
{
|
44
|
+
subject: subject_name,
|
45
|
+
id: 1,
|
46
|
+
version: 1,
|
47
|
+
schema: schema
|
48
|
+
}
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'caches the result of subject_version' do
|
52
|
+
allow(upstream).to receive(:subject_version).with(subject_name, version).and_return(schema_with_meta)
|
53
|
+
registry.subject_version(subject_name, version)
|
54
|
+
registry.subject_version(subject_name, version)
|
55
|
+
expect(upstream).to have_received(:subject_version).exactly(1).times
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
37
59
|
it_behaves_like "a confluent schema registry client" do
|
38
60
|
let(:upstream) { AvroTurf::ConfluentSchemaRegistry.new(registry_url, logger: logger) }
|
39
61
|
let(:registry) { described_class.new(upstream) }
|
@@ -0,0 +1,159 @@
|
|
1
|
+
require 'webmock/rspec'
|
2
|
+
require 'avro_turf/cached_confluent_schema_registry'
|
3
|
+
require 'avro_turf/test/fake_confluent_schema_registry_server'
|
4
|
+
|
5
|
+
describe AvroTurf::CachedConfluentSchemaRegistry do
|
6
|
+
let(:upstream) { instance_double(AvroTurf::ConfluentSchemaRegistry) }
|
7
|
+
let(:cache) { AvroTurf::DiskCache.new("spec/cache")}
|
8
|
+
let(:registry) { described_class.new(upstream, cache: cache) }
|
9
|
+
let(:id) { rand(999) }
|
10
|
+
let(:schema) do
|
11
|
+
{
|
12
|
+
type: "record",
|
13
|
+
name: "person",
|
14
|
+
fields: [{ name: "name", type: "string" }]
|
15
|
+
}.to_json
|
16
|
+
end
|
17
|
+
|
18
|
+
let(:city_id) { rand(999) }
|
19
|
+
let(:city_schema) do
|
20
|
+
{
|
21
|
+
type: "record",
|
22
|
+
name: "city",
|
23
|
+
fields: [{ name: "name", type: "string" }]
|
24
|
+
}.to_json
|
25
|
+
end
|
26
|
+
|
27
|
+
let(:subject) { 'subject' }
|
28
|
+
let(:version) { rand(999) }
|
29
|
+
let(:subject_version_schema) do
|
30
|
+
{
|
31
|
+
subject: subject,
|
32
|
+
version: version,
|
33
|
+
id: id,
|
34
|
+
schema: {
|
35
|
+
type: "record",
|
36
|
+
name: "city",
|
37
|
+
fields: { name: "name", type: "string" }
|
38
|
+
}
|
39
|
+
}.to_json
|
40
|
+
end
|
41
|
+
|
42
|
+
before do
|
43
|
+
FileUtils.mkdir_p("spec/cache")
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "#fetch" do
|
47
|
+
let(:cache_before) do
|
48
|
+
{
|
49
|
+
"#{id}" => "#{schema}"
|
50
|
+
}
|
51
|
+
end
|
52
|
+
let(:cache_after) do
|
53
|
+
{
|
54
|
+
"#{id}" => "#{schema}",
|
55
|
+
"#{city_id}" => "#{city_schema}"
|
56
|
+
}
|
57
|
+
end
|
58
|
+
|
59
|
+
# setup the disk cache to avoid performing the upstream fetch
|
60
|
+
before do
|
61
|
+
store_cache("schemas_by_id.json", cache_before)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "uses preloaded disk cache" do
|
65
|
+
# multiple calls return same result, with zero upstream calls
|
66
|
+
allow(upstream).to receive(:fetch).with(id).and_return(schema)
|
67
|
+
expect(registry.fetch(id)).to eq(schema)
|
68
|
+
expect(registry.fetch(id)).to eq(schema)
|
69
|
+
expect(upstream).to have_received(:fetch).exactly(0).times
|
70
|
+
expect(load_cache("schemas_by_id.json")).to eq cache_before
|
71
|
+
end
|
72
|
+
|
73
|
+
it "writes thru to disk cache" do
|
74
|
+
# multiple calls return same result, with only one upstream call
|
75
|
+
allow(upstream).to receive(:fetch).with(city_id).and_return(city_schema)
|
76
|
+
expect(registry.fetch(city_id)).to eq(city_schema)
|
77
|
+
expect(registry.fetch(city_id)).to eq(city_schema)
|
78
|
+
expect(upstream).to have_received(:fetch).exactly(1).times
|
79
|
+
expect(load_cache("schemas_by_id.json")).to eq cache_after
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "#register" do
|
84
|
+
let(:subject_name) { "a_subject" }
|
85
|
+
let(:cache_before) do
|
86
|
+
{
|
87
|
+
"#{subject_name}#{schema}" => id
|
88
|
+
}
|
89
|
+
end
|
90
|
+
|
91
|
+
let(:city_name) { "a_city" }
|
92
|
+
let(:cache_after) do
|
93
|
+
{
|
94
|
+
"#{subject_name}#{schema}" => id,
|
95
|
+
"#{city_name}#{city_schema}" => city_id
|
96
|
+
}
|
97
|
+
end
|
98
|
+
|
99
|
+
# setup the disk cache to avoid performing the upstream register
|
100
|
+
before do
|
101
|
+
store_cache("ids_by_schema.json", cache_before)
|
102
|
+
end
|
103
|
+
|
104
|
+
it "uses preloaded disk cache" do
|
105
|
+
# multiple calls return same result, with zero upstream calls
|
106
|
+
allow(upstream).to receive(:register).with(subject_name, schema).and_return(id)
|
107
|
+
expect(registry.register(subject_name, schema)).to eq(id)
|
108
|
+
expect(registry.register(subject_name, schema)).to eq(id)
|
109
|
+
expect(upstream).to have_received(:register).exactly(0).times
|
110
|
+
expect(load_cache("ids_by_schema.json")).to eq cache_before
|
111
|
+
end
|
112
|
+
|
113
|
+
it "writes thru to disk cache" do
|
114
|
+
# multiple calls return same result, with only one upstream call
|
115
|
+
allow(upstream).to receive(:register).with(city_name, city_schema).and_return(city_id)
|
116
|
+
expect(registry.register(city_name, city_schema)).to eq(city_id)
|
117
|
+
expect(registry.register(city_name, city_schema)).to eq(city_id)
|
118
|
+
expect(upstream).to have_received(:register).exactly(1).times
|
119
|
+
expect(load_cache("ids_by_schema.json")).to eq cache_after
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
describe "#subject_version" do
|
124
|
+
it "writes thru to disk cache" do
|
125
|
+
# multiple calls return same result, with zero upstream calls
|
126
|
+
allow(upstream).to receive(:subject_version).with(subject, version).and_return(subject_version_schema)
|
127
|
+
expect(File).not_to exist("./spec/cache/schemas_by_subject_version.json")
|
128
|
+
|
129
|
+
expect(registry.subject_version(subject, version)).to eq(subject_version_schema)
|
130
|
+
|
131
|
+
json = JSON.parse(File.read("./spec/cache/schemas_by_subject_version.json"))["#{subject}#{version}"]
|
132
|
+
expect(json).to eq(subject_version_schema)
|
133
|
+
|
134
|
+
expect(registry.subject_version(subject, version)).to eq(subject_version_schema)
|
135
|
+
expect(upstream).to have_received(:subject_version).exactly(1).times
|
136
|
+
end
|
137
|
+
|
138
|
+
it "reads from disk cache and populates mem cache" do
|
139
|
+
allow(upstream).to receive(:subject_version).with(subject, version).and_return(subject_version_schema)
|
140
|
+
key = "#{subject}#{version}"
|
141
|
+
hash = {key => subject_version_schema}
|
142
|
+
cache.send(:write_to_disk_cache, "./spec/cache/schemas_by_subject_version.json", hash)
|
143
|
+
|
144
|
+
cached_schema = cache.instance_variable_get(:@schemas_by_subject_version)
|
145
|
+
expect(cached_schema).to eq({})
|
146
|
+
|
147
|
+
expect(registry.subject_version(subject, version)).to eq(subject_version_schema)
|
148
|
+
expect(upstream).to have_received(:subject_version).exactly(0).times
|
149
|
+
|
150
|
+
cached_schema = cache.instance_variable_get(:@schemas_by_subject_version)
|
151
|
+
expect(cached_schema).to eq({key => subject_version_schema})
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
it_behaves_like "a confluent schema registry client" do
|
156
|
+
let(:upstream) { AvroTurf::ConfluentSchemaRegistry.new(registry_url, logger: logger) }
|
157
|
+
let(:registry) { described_class.new(upstream) }
|
158
|
+
end
|
159
|
+
end
|
data/spec/messaging_spec.rb
CHANGED
@@ -15,18 +15,8 @@ describe AvroTurf::Messaging do
|
|
15
15
|
}
|
16
16
|
|
17
17
|
let(:message) { { "full_name" => "John Doe" } }
|
18
|
-
|
19
|
-
|
20
|
-
FileUtils.mkdir_p("spec/schemas")
|
21
|
-
end
|
22
|
-
|
23
|
-
before do
|
24
|
-
stub_request(:any, /^#{registry_url}/).to_rack(FakeConfluentSchemaRegistryServer)
|
25
|
-
FakeConfluentSchemaRegistryServer.clear
|
26
|
-
end
|
27
|
-
|
28
|
-
before do
|
29
|
-
define_schema "person.avsc", <<-AVSC
|
18
|
+
let(:schema_json) do
|
19
|
+
<<-AVSC
|
30
20
|
{
|
31
21
|
"name": "person",
|
32
22
|
"type": "record",
|
@@ -39,8 +29,22 @@ describe AvroTurf::Messaging do
|
|
39
29
|
}
|
40
30
|
AVSC
|
41
31
|
end
|
32
|
+
let(:schema) { Avro::Schema.parse(schema_json) }
|
42
33
|
|
43
|
-
|
34
|
+
before do
|
35
|
+
FileUtils.mkdir_p("spec/schemas")
|
36
|
+
end
|
37
|
+
|
38
|
+
before do
|
39
|
+
stub_request(:any, /^#{registry_url}/).to_rack(FakeConfluentSchemaRegistryServer)
|
40
|
+
FakeConfluentSchemaRegistryServer.clear
|
41
|
+
end
|
42
|
+
|
43
|
+
before do
|
44
|
+
define_schema "person.avsc", schema_json
|
45
|
+
end
|
46
|
+
|
47
|
+
shared_examples_for "encoding and decoding with the schema from schema store" do
|
44
48
|
it "encodes and decodes messages" do
|
45
49
|
data = avro.encode(message, schema_name: "person")
|
46
50
|
expect(avro.decode(data)).to eq message
|
@@ -60,7 +64,66 @@ describe AvroTurf::Messaging do
|
|
60
64
|
end
|
61
65
|
end
|
62
66
|
|
63
|
-
|
67
|
+
shared_examples_for 'encoding and decoding with the schema from registry' do
|
68
|
+
before do
|
69
|
+
registry = AvroTurf::ConfluentSchemaRegistry.new(registry_url, logger: logger)
|
70
|
+
registry.register('person', schema)
|
71
|
+
registry.register('people', schema)
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'encodes and decodes messages' do
|
75
|
+
data = avro.encode(message, subject: 'person', version: 1)
|
76
|
+
expect(avro.decode(data)).to eq message
|
77
|
+
end
|
78
|
+
|
79
|
+
it "allows specifying a reader's schema by subject and version" do
|
80
|
+
data = avro.encode(message, subject: 'person', version: 1)
|
81
|
+
expect(avro.decode(data, schema_name: 'person')).to eq message
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'raises AvroTurf::SchemaNotFoundError when the schema does not exist on registry' do
|
85
|
+
expect { avro.encode(message, subject: 'missing', version: 1) }.to raise_error(AvroTurf::SchemaNotFoundError)
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'caches parsed schemas for decoding' do
|
89
|
+
data = avro.encode(message, subject: 'person', version: 1)
|
90
|
+
avro.decode(data)
|
91
|
+
allow(Avro::Schema).to receive(:parse).and_call_original
|
92
|
+
expect(avro.decode(data)).to eq message
|
93
|
+
expect(Avro::Schema).not_to have_received(:parse)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
shared_examples_for 'encoding and decoding with the schema_id from registry' do
|
98
|
+
before do
|
99
|
+
registry = AvroTurf::ConfluentSchemaRegistry.new(registry_url, logger: logger)
|
100
|
+
registry.register('person', schema)
|
101
|
+
registry.register('people', schema)
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'encodes and decodes messages' do
|
105
|
+
data = avro.encode(message, schema_id: 1)
|
106
|
+
expect(avro.decode(data)).to eq message
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'raises AvroTurf::SchemaNotFoundError when the schema does not exist on registry' do
|
110
|
+
expect { avro.encode(message, schema_id: 5) }.to raise_error(AvroTurf::SchemaNotFoundError)
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'caches parsed schemas for decoding' do
|
114
|
+
data = avro.encode(message, schema_id: 1)
|
115
|
+
avro.decode(data)
|
116
|
+
allow(Avro::Schema).to receive(:parse).and_call_original
|
117
|
+
expect(avro.decode(data)).to eq message
|
118
|
+
expect(Avro::Schema).not_to have_received(:parse)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
it_behaves_like "encoding and decoding with the schema from schema store"
|
123
|
+
|
124
|
+
it_behaves_like 'encoding and decoding with the schema from registry'
|
125
|
+
|
126
|
+
it_behaves_like 'encoding and decoding with the schema_id from registry'
|
64
127
|
|
65
128
|
context "with a provided registry" do
|
66
129
|
let(:registry) { AvroTurf::ConfluentSchemaRegistry.new(registry_url, logger: logger) }
|
@@ -73,7 +136,11 @@ describe AvroTurf::Messaging do
|
|
73
136
|
)
|
74
137
|
end
|
75
138
|
|
76
|
-
it_behaves_like "encoding and decoding"
|
139
|
+
it_behaves_like "encoding and decoding with the schema from schema store"
|
140
|
+
|
141
|
+
it_behaves_like 'encoding and decoding with the schema from registry'
|
142
|
+
|
143
|
+
it_behaves_like 'encoding and decoding with the schema_id from registry'
|
77
144
|
|
78
145
|
it "uses the provided registry" do
|
79
146
|
allow(registry).to receive(:register).and_call_original
|
@@ -101,7 +168,7 @@ describe AvroTurf::Messaging do
|
|
101
168
|
)
|
102
169
|
end
|
103
170
|
|
104
|
-
it_behaves_like "encoding and decoding"
|
171
|
+
it_behaves_like "encoding and decoding with the schema from schema store"
|
105
172
|
|
106
173
|
it "uses the provided schema store" do
|
107
174
|
allow(schema_store).to receive(:find).and_call_original
|
@@ -109,4 +176,119 @@ describe AvroTurf::Messaging do
|
|
109
176
|
expect(schema_store).to have_received(:find).with("person", nil)
|
110
177
|
end
|
111
178
|
end
|
179
|
+
|
180
|
+
describe 'decoding with #decode_message' do
|
181
|
+
shared_examples_for "encoding and decoding with the schema from schema store" do
|
182
|
+
it "encodes and decodes messages" do
|
183
|
+
data = avro.encode(message, schema_name: "person")
|
184
|
+
result = avro.decode_message(data)
|
185
|
+
expect(result.message).to eq message
|
186
|
+
expect(result.schema_id).to eq 0
|
187
|
+
expect(result.writer_schema).to eq schema
|
188
|
+
expect(result.reader_schema).to eq nil
|
189
|
+
end
|
190
|
+
|
191
|
+
it "allows specifying a reader's schema" do
|
192
|
+
data = avro.encode(message, schema_name: "person")
|
193
|
+
result = avro.decode_message(data, schema_name: "person")
|
194
|
+
expect(result.message).to eq message
|
195
|
+
expect(result.writer_schema).to eq schema
|
196
|
+
expect(result.reader_schema).to eq schema
|
197
|
+
end
|
198
|
+
|
199
|
+
it "caches parsed schemas for decoding" do
|
200
|
+
data = avro.encode(message, schema_name: "person")
|
201
|
+
avro.decode_message(data)
|
202
|
+
allow(Avro::Schema).to receive(:parse).and_call_original
|
203
|
+
expect(avro.decode_message(data).message).to eq message
|
204
|
+
expect(Avro::Schema).not_to have_received(:parse)
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
shared_examples_for 'encoding and decoding with the schema from registry' do
|
209
|
+
before do
|
210
|
+
registry = AvroTurf::ConfluentSchemaRegistry.new(registry_url, logger: logger)
|
211
|
+
registry.register('person', schema)
|
212
|
+
registry.register('people', schema)
|
213
|
+
end
|
214
|
+
|
215
|
+
it 'encodes and decodes messages' do
|
216
|
+
data = avro.encode(message, subject: 'person', version: 1)
|
217
|
+
result = avro.decode_message(data)
|
218
|
+
expect(result.message).to eq message
|
219
|
+
expect(result.schema_id).to eq 0
|
220
|
+
end
|
221
|
+
|
222
|
+
it "allows specifying a reader's schema by subject and version" do
|
223
|
+
data = avro.encode(message, subject: 'person', version: 1)
|
224
|
+
expect(avro.decode_message(data, schema_name: 'person').message).to eq message
|
225
|
+
end
|
226
|
+
|
227
|
+
it 'raises AvroTurf::SchemaNotFoundError when the schema does not exist on registry' do
|
228
|
+
expect { avro.encode(message, subject: 'missing', version: 1) }.to raise_error(AvroTurf::SchemaNotFoundError)
|
229
|
+
end
|
230
|
+
|
231
|
+
it 'caches parsed schemas for decoding' do
|
232
|
+
data = avro.encode(message, subject: 'person', version: 1)
|
233
|
+
avro.decode_message(data)
|
234
|
+
allow(Avro::Schema).to receive(:parse).and_call_original
|
235
|
+
expect(avro.decode_message(data).message).to eq message
|
236
|
+
expect(Avro::Schema).not_to have_received(:parse)
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
it_behaves_like "encoding and decoding with the schema from schema store"
|
241
|
+
|
242
|
+
it_behaves_like 'encoding and decoding with the schema from registry'
|
243
|
+
|
244
|
+
context "with a provided registry" do
|
245
|
+
let(:registry) { AvroTurf::ConfluentSchemaRegistry.new(registry_url, logger: logger) }
|
246
|
+
|
247
|
+
let(:avro) do
|
248
|
+
AvroTurf::Messaging.new(
|
249
|
+
registry: registry,
|
250
|
+
schemas_path: "spec/schemas",
|
251
|
+
logger: logger
|
252
|
+
)
|
253
|
+
end
|
254
|
+
|
255
|
+
it_behaves_like "encoding and decoding with the schema from schema store"
|
256
|
+
|
257
|
+
it_behaves_like 'encoding and decoding with the schema from registry'
|
258
|
+
|
259
|
+
it "uses the provided registry" do
|
260
|
+
allow(registry).to receive(:register).and_call_original
|
261
|
+
message = { "full_name" => "John Doe" }
|
262
|
+
avro.encode(message, schema_name: "person")
|
263
|
+
expect(registry).to have_received(:register).with("person", anything)
|
264
|
+
end
|
265
|
+
|
266
|
+
it "allows specifying a schema registry subject" do
|
267
|
+
allow(registry).to receive(:register).and_call_original
|
268
|
+
message = { "full_name" => "John Doe" }
|
269
|
+
avro.encode(message, schema_name: "person", subject: "people")
|
270
|
+
expect(registry).to have_received(:register).with("people", anything)
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
context "with a provided schema store" do
|
275
|
+
let(:schema_store) { AvroTurf::SchemaStore.new(path: "spec/schemas") }
|
276
|
+
|
277
|
+
let(:avro) do
|
278
|
+
AvroTurf::Messaging.new(
|
279
|
+
registry_url: registry_url,
|
280
|
+
schema_store: schema_store,
|
281
|
+
logger: logger
|
282
|
+
)
|
283
|
+
end
|
284
|
+
|
285
|
+
it_behaves_like "encoding and decoding with the schema from schema store"
|
286
|
+
|
287
|
+
it "uses the provided schema store" do
|
288
|
+
allow(schema_store).to receive(:find).and_call_original
|
289
|
+
avro.encode(message, schema_name: "person")
|
290
|
+
expect(schema_store).to have_received(:find).with("person", nil)
|
291
|
+
end
|
292
|
+
end
|
293
|
+
end
|
112
294
|
end
|
data/spec/schema_store_spec.rb
CHANGED
@@ -197,6 +197,42 @@ describe AvroTurf::SchemaStore do
|
|
197
197
|
schema = store.find("person")
|
198
198
|
expect(schema.fullname).to eq "person"
|
199
199
|
end
|
200
|
+
|
201
|
+
it "is thread safe" do
|
202
|
+
define_schema "address.avsc", <<-AVSC
|
203
|
+
{
|
204
|
+
"type": "record",
|
205
|
+
"name": "address",
|
206
|
+
"fields": []
|
207
|
+
}
|
208
|
+
AVSC
|
209
|
+
|
210
|
+
# Set a Thread breakpoint right in the core place of race condition
|
211
|
+
expect(Avro::Name)
|
212
|
+
.to receive(:add_name)
|
213
|
+
.and_wrap_original { |m, *args|
|
214
|
+
Thread.stop
|
215
|
+
m.call(*args)
|
216
|
+
}
|
217
|
+
|
218
|
+
# Run two concurring threads which both will trigger the same schema loading
|
219
|
+
threads = 2.times.map { Thread.new { store.find("address") } }
|
220
|
+
# Wait for the moment when both threads will reach the breakpoint
|
221
|
+
sleep 0.001 until threads.all?(&:stop?)
|
222
|
+
|
223
|
+
expect {
|
224
|
+
# Resume the threads evaluation, one after one
|
225
|
+
threads.each do |thread|
|
226
|
+
next unless thread.status == 'sleep'
|
227
|
+
|
228
|
+
thread.run
|
229
|
+
sleep 0.001 until thread.stop?
|
230
|
+
end
|
231
|
+
|
232
|
+
# Ensure that threads are finished
|
233
|
+
threads.each(&:join)
|
234
|
+
}.to_not raise_error
|
235
|
+
end
|
200
236
|
end
|
201
237
|
|
202
238
|
describe "#load_schemas!" do
|
data/spec/spec_helper.rb
CHANGED
@@ -12,6 +12,14 @@ module Helpers
|
|
12
12
|
f.write(content)
|
13
13
|
end
|
14
14
|
end
|
15
|
+
|
16
|
+
def store_cache(path, hash)
|
17
|
+
File.write(File.join("spec/cache", path), JSON.generate(hash))
|
18
|
+
end
|
19
|
+
|
20
|
+
def load_cache(path)
|
21
|
+
JSON.parse(File.read(File.join("spec/cache", path)))
|
22
|
+
end
|
15
23
|
end
|
16
24
|
|
17
25
|
RSpec.configure do |config|
|
@@ -88,16 +88,18 @@ shared_examples_for "a confluent schema registry client" do
|
|
88
88
|
end
|
89
89
|
|
90
90
|
describe "#subject_version" do
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
91
|
+
let!(:schema_id1) do
|
92
|
+
registry.register(subject_name, { type: :record, name: "r0", fields: [] }.to_json)
|
93
|
+
end
|
94
|
+
let!(:schema_id2) do
|
95
|
+
registry.register(subject_name, { type: :record, name: "r1", fields: [] }.to_json)
|
96
96
|
end
|
97
|
+
|
97
98
|
let(:expected) do
|
98
99
|
{
|
99
100
|
name: subject_name,
|
100
101
|
version: 1,
|
102
|
+
id: schema_id1,
|
101
103
|
schema: { type: :record, name: "r0", fields: [] }.to_json
|
102
104
|
}.to_json
|
103
105
|
end
|
@@ -112,6 +114,7 @@ shared_examples_for "a confluent schema registry client" do
|
|
112
114
|
{
|
113
115
|
name: subject_name,
|
114
116
|
version: 2,
|
117
|
+
id: schema_id2,
|
115
118
|
schema: { type: :record, name: "r1", fields: [] }.to_json
|
116
119
|
}.to_json
|
117
120
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'rack/test'
|
2
|
+
require 'avro_turf/test/fake_confluent_schema_registry_server'
|
3
|
+
|
4
|
+
describe FakeConfluentSchemaRegistryServer do
|
5
|
+
include Rack::Test::Methods
|
6
|
+
|
7
|
+
def app; described_class; end
|
8
|
+
|
9
|
+
let(:schema) do
|
10
|
+
{
|
11
|
+
type: "record",
|
12
|
+
name: "person",
|
13
|
+
fields: [
|
14
|
+
{ name: "name", type: "string" }
|
15
|
+
]
|
16
|
+
}.to_json
|
17
|
+
end
|
18
|
+
|
19
|
+
describe 'POST /subjects/:subject/versions' do
|
20
|
+
it 'returns the same schema ID when invoked with same schema and same subject' do
|
21
|
+
post '/subjects/person/versions', { schema: schema }.to_json, 'CONTENT_TYPE' => 'application/vnd.schemaregistry+json'
|
22
|
+
|
23
|
+
expected_id = JSON.parse(last_response.body).fetch('id')
|
24
|
+
|
25
|
+
post '/subjects/person/versions', { schema: schema }.to_json, 'CONTENT_TYPE' => 'application/vnd.schemaregistry+json'
|
26
|
+
|
27
|
+
expect(JSON.parse(last_response.body).fetch('id')).to eq expected_id
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'returns a different schema ID when invoked with same schema and different subject' do
|
31
|
+
post '/subjects/person/versions', { schema: schema }.to_json, 'CONTENT_TYPE' => 'application/vnd.schemaregistry+json'
|
32
|
+
|
33
|
+
original_id = JSON.parse(last_response.body).fetch('id')
|
34
|
+
|
35
|
+
post '/subjects/happy-person/versions', { schema: schema }.to_json, 'CONTENT_TYPE' => 'application/vnd.schemaregistry+json'
|
36
|
+
|
37
|
+
expect(JSON.parse(last_response.body).fetch('id')).not_to eq original_id
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: avro_turf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Schierbeck
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-09-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: avro
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: 1.7.7
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '1.
|
22
|
+
version: '1.10'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,7 +29,7 @@ dependencies:
|
|
29
29
|
version: 1.7.7
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '1.
|
32
|
+
version: '1.10'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: excon
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -50,14 +50,14 @@ dependencies:
|
|
50
50
|
requirements:
|
51
51
|
- - "~>"
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version: '
|
53
|
+
version: '2.0'
|
54
54
|
type: :development
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
57
|
requirements:
|
58
58
|
- - "~>"
|
59
59
|
- !ruby/object:Gem::Version
|
60
|
-
version: '
|
60
|
+
version: '2.0'
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
62
|
name: rake
|
63
63
|
requirement: !ruby/object:Gem::Requirement
|
@@ -142,6 +142,20 @@ dependencies:
|
|
142
142
|
- - ">="
|
143
143
|
- !ruby/object:Gem::Version
|
144
144
|
version: '0'
|
145
|
+
- !ruby/object:Gem::Dependency
|
146
|
+
name: rack-test
|
147
|
+
requirement: !ruby/object:Gem::Requirement
|
148
|
+
requirements:
|
149
|
+
- - ">="
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: '0'
|
152
|
+
type: :development
|
153
|
+
prerelease: false
|
154
|
+
version_requirements: !ruby/object:Gem::Requirement
|
155
|
+
requirements:
|
156
|
+
- - ">="
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
version: '0'
|
145
159
|
description:
|
146
160
|
email:
|
147
161
|
- dasch@zendesk.com
|
@@ -149,6 +163,8 @@ executables: []
|
|
149
163
|
extensions: []
|
150
164
|
extra_rdoc_files: []
|
151
165
|
files:
|
166
|
+
- ".circleci/config.yml"
|
167
|
+
- ".github/workflows/ruby.yml"
|
152
168
|
- ".gitignore"
|
153
169
|
- ".rspec"
|
154
170
|
- CHANGELOG.md
|
@@ -157,7 +173,6 @@ files:
|
|
157
173
|
- README.md
|
158
174
|
- Rakefile
|
159
175
|
- avro_turf.gemspec
|
160
|
-
- circle.yml
|
161
176
|
- lib/avro_turf.rb
|
162
177
|
- lib/avro_turf/cached_confluent_schema_registry.rb
|
163
178
|
- lib/avro_turf/cached_schema_registry.rb
|
@@ -173,6 +188,8 @@ files:
|
|
173
188
|
- lib/avro_turf/core_ext/symbol.rb
|
174
189
|
- lib/avro_turf/core_ext/time.rb
|
175
190
|
- lib/avro_turf/core_ext/true_class.rb
|
191
|
+
- lib/avro_turf/disk_cache.rb
|
192
|
+
- lib/avro_turf/in_memory_cache.rb
|
176
193
|
- lib/avro_turf/messaging.rb
|
177
194
|
- lib/avro_turf/mutable_schema_store.rb
|
178
195
|
- lib/avro_turf/schema_registry.rb
|
@@ -198,11 +215,13 @@ files:
|
|
198
215
|
- spec/core_ext/symbol_spec.rb
|
199
216
|
- spec/core_ext/time_spec.rb
|
200
217
|
- spec/core_ext/true_class_spec.rb
|
218
|
+
- spec/disk_cached_confluent_schema_registry_spec.rb
|
201
219
|
- spec/messaging_spec.rb
|
202
220
|
- spec/schema_store_spec.rb
|
203
221
|
- spec/schema_to_avro_patch_spec.rb
|
204
222
|
- spec/spec_helper.rb
|
205
223
|
- spec/support/confluent_schema_registry_context.rb
|
224
|
+
- spec/test/fake_confluent_schema_registry_server_spec.rb
|
206
225
|
homepage: https://github.com/dasch/avro_turf
|
207
226
|
licenses:
|
208
227
|
- MIT
|
@@ -250,8 +269,10 @@ test_files:
|
|
250
269
|
- spec/core_ext/symbol_spec.rb
|
251
270
|
- spec/core_ext/time_spec.rb
|
252
271
|
- spec/core_ext/true_class_spec.rb
|
272
|
+
- spec/disk_cached_confluent_schema_registry_spec.rb
|
253
273
|
- spec/messaging_spec.rb
|
254
274
|
- spec/schema_store_spec.rb
|
255
275
|
- spec/schema_to_avro_patch_spec.rb
|
256
276
|
- spec/spec_helper.rb
|
257
277
|
- spec/support/confluent_schema_registry_context.rb
|
278
|
+
- spec/test/fake_confluent_schema_registry_server_spec.rb
|
data/circle.yml
DELETED