Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize ddoc cache #610

Merged
merged 2 commits into from
Aug 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions rel/overlay/etc/default.ini
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ credentials = false
; List of accepted methods
; methods =

; Configuration for the design document cache
;[ddoc_cache]
; The maximum size of the cache in bytes
;max_size = 104857600 ; 100MiB
; The period each cache entry should wait before
; automatically refreshing in milliseconds
;refresh_timeout = 67000

[x_frame_options]
; Settings same-origin will return X-Frame-Options: SAMEORIGIN.
; If same origin is set, it will ignore the hosts setting
Expand Down
13 changes: 2 additions & 11 deletions src/couch/src/couch_db_updater.erl
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ handle_info({update_docs, Client, GroupedDocs, NonRepDocs, MergeConflicts,
couch_event:notify(Db2#db.name, {ddoc_updated, DDocId})
end, UpdatedDDocIds),
couch_event:notify(Db2#db.name, ddoc_updated),
ddoc_cache:evict(Db2#db.name, UpdatedDDocIds),
ddoc_cache:refresh(Db2#db.name, UpdatedDDocIds),
refresh_validate_doc_funs(Db2);
false ->
Db2
Expand Down Expand Up @@ -923,16 +923,7 @@ update_docs_int(Db, DocsList, NonRepDocs, MergeConflicts, FullCommit) ->
(_) -> []
end, Ids),

Db4 = case length(UpdatedDDocIds) > 0 of
true ->
couch_event:notify(Db3#db.name, ddoc_updated),
ddoc_cache:evict(Db3#db.name, UpdatedDDocIds),
refresh_validate_doc_funs(Db3);
false ->
Db3
end,

{ok, commit_data(Db4, not FullCommit), UpdatedDDocIds}.
{ok, commit_data(Db3, not FullCommit), UpdatedDDocIds}.

update_local_docs(Db, []) ->
{ok, Db};
Expand Down
22 changes: 5 additions & 17 deletions src/ddoc_cache/src/ddoc_cache.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@
{application, ddoc_cache, [
{description, "Design Document Cache"},
{vsn, git},
{modules, [
ddoc_cache,
ddoc_cache_app,
ddoc_cache_opener,
ddoc_cache_sup,
ddoc_cache_util
]},
{registered, [
ddoc_cache_lru,
ddoc_cache_opener
Expand All @@ -28,17 +21,12 @@
kernel,
stdlib,
crypto,
config,
couch_event,
ets_lru,
mem3,
fabric,
couch_log,
couch_stats
couch_stats,
mem3,
fabric
]},
{mod, {ddoc_cache_app, []}},
{env, [
{max_objects, unlimited},
{max_size, 104857600}, % 100M
{max_lifetime, 60000} % 1m
]}
{mod, {ddoc_cache_app, []}}
]}.
79 changes: 18 additions & 61 deletions src/ddoc_cache/src/ddoc_cache.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,86 +12,43 @@

-module(ddoc_cache).

-export([
start/0,
stop/0
]).

-export([
open_doc/2,
open_doc/3,
open_validation_funs/1,
evict/2,
open_custom/2,
refresh/2,

%% deprecated
open/2
]).

start() ->
application:start(ddoc_cache).

stop() ->
application:stop(ddoc_cache).

open_doc(DbName, DocId) ->
Key = {DbName, DocId, '_'},
case ddoc_cache_opener:match_newest(Key) of
{ok, _} = Resp ->
couch_stats:increment_counter([ddoc_cache, hit]),
Resp;
missing ->
couch_stats:increment_counter([ddoc_cache, miss]),
ddoc_cache_opener:open_doc(DbName, DocId);
recover ->
couch_stats:increment_counter([ddoc_cache, recovery]),
ddoc_cache_opener:recover_doc(DbName, DocId)
end.
Key = {ddoc_cache_entry_ddocid, {DbName, DocId}},
ddoc_cache_lru:open(Key).


open_doc(DbName, DocId, RevId) ->
Key = {DbName, DocId, RevId},
case ddoc_cache_opener:lookup(Key) of
{ok, _} = Resp ->
couch_stats:increment_counter([ddoc_cache, hit]),
Resp;
missing ->
couch_stats:increment_counter([ddoc_cache, miss]),
ddoc_cache_opener:open_doc(DbName, DocId, RevId);
recover ->
couch_stats:increment_counter([ddoc_cache, recovery]),
ddoc_cache_opener:recover_doc(DbName, DocId, RevId)
end.
Key = {ddoc_cache_entry_ddocid_rev, {DbName, DocId, RevId}},
ddoc_cache_lru:open(Key).


open_validation_funs(DbName) ->
Key = {DbName, validation_funs},
case ddoc_cache_opener:lookup(Key) of
{ok, _} = Resp ->
couch_stats:increment_counter([ddoc_cache, hit]),
Resp;
missing ->
couch_stats:increment_counter([ddoc_cache, miss]),
ddoc_cache_opener:open_validation_funs(DbName);
recover ->
couch_stats:increment_counter([ddoc_cache, recovery]),
ddoc_cache_opener:recover_validation_funs(DbName)
end.
Key = {ddoc_cache_entry_validation_funs, DbName},
ddoc_cache_lru:open(Key).


open_custom(DbName, Mod) ->
Key = {DbName, Mod},
case ddoc_cache_opener:lookup(Key) of
{ok, _} = Resp ->
couch_stats:increment_counter([ddoc_cache, hit]),
Resp;
missing ->
couch_stats:increment_counter([ddoc_cache, miss]),
ddoc_cache_opener:open_doc(DbName, Mod);
recover ->
couch_stats:increment_counter([ddoc_cache, recovery]),
Mod:recover(DbName)
end.

evict(ShardDbName, DDocIds) ->
Key = {ddoc_cache_entry_custom, {DbName, Mod}},
ddoc_cache_lru:open(Key).


refresh(ShardDbName, DDocIds) when is_list(DDocIds) ->
DbName = mem3:dbname(ShardDbName),
ddoc_cache_opener:evict_docs(DbName, DDocIds).
ddoc_cache_lru:refresh(DbName, DDocIds).


open(DbName, validation_funs) ->
open_validation_funs(DbName);
Expand Down
40 changes: 40 additions & 0 deletions src/ddoc_cache/src/ddoc_cache.hrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
% Licensed 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.

-type dbname() :: iodata().
-type docid() :: iodata().
-type doc_hash() :: <<_:128>>.
-type revision() :: {pos_integer(), doc_hash()}.

-define(CACHE, ddoc_cache_entries).
-define(LRU, ddoc_cache_lru).
-define(REFRESH_TIMEOUT, 67000).
-define(SHUTDOWN_TIMEOUT, 1000).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to not make these configurable? At the very least REFRESH_TIMEOUT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to make them configurable?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made REFRESH_TIMEOUT configurable and this is now just the default. SHUTDOWN_TIMEOUT is entirely too subtle to be turned into a config option. If we hit issues there then we have other problems.


-record(entry, {
key,
val,
pid
}).

-record(opener, {
key,
pid,
clients
}).


-ifdef(TEST).
-define(EVENT(Name, Arg), ddoc_cache_ev:event(Name, Arg)).
-else.
-define(EVENT(Name, Arg), ignore).
-endif.