Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save binarytemple/537d73aeb997fdeecffd to your computer and use it in GitHub Desktop.

Select an option

Save binarytemple/537d73aeb997fdeecffd to your computer and use it in GitHub Desktop.
Exploring Riak/Yokozuna/Solr sharing and communication of search index schema files.

Question

Can I change Yokozuna schema just be editing the schema files in the data/yz/.. directory?

Answer

No. On upload, data is broadcast to all nodes by riak_core_metadata:put and also stored to a DETS database in the data/metadata directory on each node.

I know from previous research that such files can be located/read using the following code.

{ok,N} = dets:open_file(schema, [{file,"/basho/riak/dev2.0/dev1/data/cluster_meta/manifest.dets"},{repair,false}]), F =
fun(X) -> io:format("~p~n", [X]), continue end, dets:traverse(N, F), dets:close(N).
{{core,bucket_types},
 {riak_core_metadata_manager,{core,bucket_types}},
 "./data/cluster_meta/A74AD8DFACD4F985EB3977517615CE25-D9AE0BDFD466ACC213487C9DCE6661AC-11.dets"}
{{yokozuna,schemas},
 {riak_core_metadata_manager,{yokozuna,schemas}},
 "./data/cluster_meta/D1D40C94833EE9B1FBDD6731CFEAA681-2FCA04A5772BC5B644E3EBC8BA85F401-11.dets"}
{{yokozuna,indexes},
 {riak_core_metadata_manager,{yokozuna,indexes}},
 "./data/cluster_meta/D1D40C94833EE9B1FBDD6731CFEAA681-626998F31A2E3A6B36816DCB3BB63AB3-11.dets"}
ok

Then when Riak instantiates a new Solr core, or when Riak reinstantiates a missing core - the schema file is read from DETS and written to <riak data directory>/yz/<index name>_index/conf/<schema name>.

Lets take a look at the code path.

Firstly the webmachine request handler function defined in yz_wm_schema.store_schema. It extracts the uploaded schema from the request and passes it down to yz_schema:store/2

 %% Responds to a PUT request by storing the schema
 %% Will overwrite schema with the same name
 store_schema(RD, S) ->
     SchemaName = S#ctx.schema_name,
     Schema = wrq:req_body(RD),
     case yz_schema:store(list_to_binary(SchemaName), Schema) of
         ok  ->
             {true, RD, S};
         {error, Reason} ->
             Msg = io_lib:format("Error storing schema: ~s~n", [Reason]),
             RD2 = wrq:append_to_response_body(Msg, RD),
             RD3 = wrq:set_resp_header("Content-Type", "text/plain", RD2),
             {{halt,400}, RD3, S}
     end.

 %% Responds to a GET request by returning schema
 read_schema(RD, S) ->
     SchemaName = S#ctx.schema_name,
     {ok, RawSchema} = yz_schema:get(list_to_binary(SchemaName)),
     {RawSchema, RD, S}.

Then the method which does the actual storage yz_schema:store/2. It compresses the schema and stores it to DETS using riak_core_metadata:put.

%% @doc Store the `RawSchema' with `Name'.
-spec store(schema_name(), raw_schema()) -> ok | schema_err().
store(Name, RawSchema) when is_binary(RawSchema) ->
    case parse_and_verify(Name, RawSchema) of
        {ok, RawSchema} ->
            CompressedSchema = yz_misc:compress(RawSchema),
            riak_core_metadata:put(?YZ_META_SCHEMAS, Name, CompressedSchema),
            ok;
        {error, _} = Err ->
            Err
    end.

%% @doc Checks if the given `SchemaName' actually exists.
-spec exists(schema_name()) -> true | false.
exists(SchemaName) ->
    case yz_schema:get(SchemaName) of
        {error, _} -> false;
        _ -> true

Perhaps a future improvement for our Solr integration could be - if Solr could be persuaded to read all it's per-index configuration including the schema directly from a Webmachine endpoint connected to the Riak Core meta-data services rather than writing the representation to the file system and coordinating access to that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment