1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
|
/* caching.c : in-memory caching
*
* ====================================================================
* 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.
* ====================================================================
*/
#include "fs.h"
#include "fs_fs.h"
#include "id.h"
#include "dag.h"
#include "tree.h"
#include "temp_serializer.h"
#include "../libsvn_fs/fs-loader.h"
#include "svn_config.h"
#include "svn_cache_config.h"
#include "svn_private_config.h"
#include "svn_hash.h"
#include "svn_pools.h"
#include "private/svn_debug.h"
#include "private/svn_subr_private.h"
/* Take the ORIGINAL string and replace all occurrences of ":" without
* limiting the key space. Allocate the result in POOL.
*/
static const char *
normalize_key_part(const char *original,
apr_pool_t *pool)
{
apr_size_t i;
apr_size_t len = strlen(original);
svn_stringbuf_t *normalized = svn_stringbuf_create_ensure(len, pool);
for (i = 0; i < len; ++i)
{
char c = original[i];
switch (c)
{
case ':': svn_stringbuf_appendbytes(normalized, "%_", 2);
break;
case '%': svn_stringbuf_appendbytes(normalized, "%%", 2);
break;
default : svn_stringbuf_appendbyte(normalized, c);
}
}
return normalized->data;
}
/* Return a memcache in *MEMCACHE_P for FS if it's configured to use
memcached, or NULL otherwise. Also, sets *FAIL_STOP to a boolean
indicating whether cache errors should be returned to the caller or
just passed to the FS warning handler.
*CACHE_TXDELTAS, *CACHE_FULLTEXTS and *CACHE_REVPROPS flags will be set
according to FS->CONFIG. *CACHE_NAMESPACE receives the cache prefix
to use.
Use FS->pool for allocating the memcache and CACHE_NAMESPACE, and POOL
for temporary allocations. */
static svn_error_t *
read_config(svn_memcache_t **memcache_p,
svn_boolean_t *fail_stop,
const char **cache_namespace,
svn_boolean_t *cache_txdeltas,
svn_boolean_t *cache_fulltexts,
svn_boolean_t *cache_revprops,
svn_fs_t *fs,
apr_pool_t *pool)
{
fs_fs_data_t *ffd = fs->fsap_data;
SVN_ERR(svn_cache__make_memcache_from_config(memcache_p, ffd->config,
fs->pool));
/* No cache namespace by default. I.e. all FS instances share the
* cached data. If you specify different namespaces, the data will
* share / compete for the same cache memory but keys will not match
* across namespaces and, thus, cached data will not be shared between
* namespaces.
*
* Since the namespace will be concatenated with other elements to form
* the complete key prefix, we must make sure that the resulting string
* is unique and cannot be created by any other combination of elements.
*/
*cache_namespace
= normalize_key_part(svn_hash__get_cstring(fs->config,
SVN_FS_CONFIG_FSFS_CACHE_NS,
""),
pool);
/* don't cache text deltas by default.
* Once we reconstructed the fulltexts from the deltas,
* these deltas are rarely re-used. Therefore, only tools
* like svnadmin will activate this to speed up operations
* dump and verify.
*/
*cache_txdeltas
= svn_hash__get_bool(fs->config,
SVN_FS_CONFIG_FSFS_CACHE_DELTAS,
FALSE);
/* by default, cache fulltexts.
* Most SVN tools care about reconstructed file content.
* Thus, this is a reasonable default.
* SVN admin tools may set that to FALSE because fulltexts
* won't be re-used rendering the cache less effective
* by squeezing wanted data out.
*/
*cache_fulltexts
= svn_hash__get_bool(fs->config,
SVN_FS_CONFIG_FSFS_CACHE_FULLTEXTS,
TRUE);
/* don't cache revprops by default.
* Revprop caching significantly speeds up operations like
* svn ls -v. However, it requires synchronization that may
* not be available or efficient in the current server setup.
*
* If the caller chose option "2", enable revprop caching if
* the required API support is there to make it efficient.
*/
if (strcmp(svn_hash__get_cstring(fs->config,
SVN_FS_CONFIG_FSFS_CACHE_REVPROPS,
""), "2"))
*cache_revprops
= svn_hash__get_bool(fs->config,
SVN_FS_CONFIG_FSFS_CACHE_REVPROPS,
FALSE);
else
*cache_revprops = svn_named_atomic__is_efficient();
return svn_config_get_bool(ffd->config, fail_stop,
CONFIG_SECTION_CACHES, CONFIG_OPTION_FAIL_STOP,
FALSE);
}
/* Implements svn_cache__error_handler_t
* This variant clears the error after logging it.
*/
static svn_error_t *
warn_and_continue_on_cache_errors(svn_error_t *err,
void *baton,
apr_pool_t *pool)
{
svn_fs_t *fs = baton;
(fs->warning)(fs->warning_baton, err);
svn_error_clear(err);
return SVN_NO_ERROR;
}
/* Implements svn_cache__error_handler_t
* This variant logs the error and passes it on to the callers.
*/
static svn_error_t *
warn_and_fail_on_cache_errors(svn_error_t *err,
void *baton,
apr_pool_t *pool)
{
svn_fs_t *fs = baton;
(fs->warning)(fs->warning_baton, err);
return err;
}
#ifdef SVN_DEBUG_CACHE_DUMP_STATS
/* Baton to be used for the dump_cache_statistics() pool cleanup function, */
struct dump_cache_baton_t
{
/* the pool about to be cleaned up. Will be used for temp. allocations. */
apr_pool_t *pool;
/* the cache to dump the statistics for */
svn_cache__t *cache;
};
/* APR pool cleanup handler that will printf the statistics of the
cache referenced by the baton in BATON_VOID. */
static apr_status_t
dump_cache_statistics(void *baton_void)
{
struct dump_cache_baton_t *baton = baton_void;
apr_status_t result = APR_SUCCESS;
svn_cache__info_t info;
svn_string_t *text_stats;
apr_array_header_t *lines;
int i;
svn_error_t *err = svn_cache__get_info(baton->cache,
&info,
TRUE,
baton->pool);
if (! err)
{
text_stats = svn_cache__format_info(&info, baton->pool);
lines = svn_cstring_split(text_stats->data, "\n", FALSE, baton->pool);
for (i = 0; i < lines->nelts; ++i)
{
const char *line = APR_ARRAY_IDX(lines, i, const char *);
#ifdef SVN_DEBUG
SVN_DBG(("%s\n", line));
#endif
}
}
/* process error returns */
if (err)
{
result = err->apr_err;
svn_error_clear(err);
}
return result;
}
#endif /* SVN_DEBUG_CACHE_DUMP_STATS */
/* This function sets / registers the required callbacks for a given
* not transaction-specific CACHE object in FS, if CACHE is not NULL.
*
* All these svn_cache__t instances shall be handled uniformly. Unless
* ERROR_HANDLER is NULL, register it for the given CACHE in FS.
*/
static svn_error_t *
init_callbacks(svn_cache__t *cache,
svn_fs_t *fs,
svn_cache__error_handler_t error_handler,
apr_pool_t *pool)
{
if (cache != NULL)
{
#ifdef SVN_DEBUG_CACHE_DUMP_STATS
/* schedule printing the access statistics upon pool cleanup,
* i.e. end of FSFS session.
*/
struct dump_cache_baton_t *baton;
baton = apr_palloc(pool, sizeof(*baton));
baton->pool = pool;
baton->cache = cache;
apr_pool_cleanup_register(pool,
baton,
dump_cache_statistics,
apr_pool_cleanup_null);
#endif
if (error_handler)
SVN_ERR(svn_cache__set_error_handler(cache,
error_handler,
fs,
pool));
}
return SVN_NO_ERROR;
}
/* Sets *CACHE_P to cache instance based on provided options.
* Creates memcache if MEMCACHE is not NULL. Creates membuffer cache if
* MEMBUFFER is not NULL. Fallbacks to inprocess cache if MEMCACHE and
* MEMBUFFER are NULL and pages is non-zero. Sets *CACHE_P to NULL
* otherwise.
*
* Unless NO_HANDLER is true, register an error handler that reports errors
* as warnings to the FS warning callback.
*
* Cache is allocated in POOL.
* */
static svn_error_t *
create_cache(svn_cache__t **cache_p,
svn_memcache_t *memcache,
svn_membuffer_t *membuffer,
apr_int64_t pages,
apr_int64_t items_per_page,
svn_cache__serialize_func_t serializer,
svn_cache__deserialize_func_t deserializer,
apr_ssize_t klen,
const char *prefix,
svn_fs_t *fs,
svn_boolean_t no_handler,
apr_pool_t *pool)
{
svn_cache__error_handler_t error_handler = no_handler
? NULL
: warn_and_fail_on_cache_errors;
if (memcache)
{
SVN_ERR(svn_cache__create_memcache(cache_p, memcache,
serializer, deserializer, klen,
prefix, pool));
error_handler = no_handler
? NULL
: warn_and_continue_on_cache_errors;
}
else if (membuffer)
{
SVN_ERR(svn_cache__create_membuffer_cache(
cache_p, membuffer, serializer, deserializer,
klen, prefix, FALSE, pool));
}
else if (pages)
{
SVN_ERR(svn_cache__create_inprocess(
cache_p, serializer, deserializer, klen, pages,
items_per_page, FALSE, prefix, pool));
}
else
{
*cache_p = NULL;
}
SVN_ERR(init_callbacks(*cache_p, fs, error_handler, pool));
return SVN_NO_ERROR;
}
svn_error_t *
svn_fs_fs__initialize_caches(svn_fs_t *fs,
apr_pool_t *pool)
{
fs_fs_data_t *ffd = fs->fsap_data;
const char *prefix = apr_pstrcat(pool,
"fsfs:", fs->uuid,
"/", normalize_key_part(fs->path, pool),
":",
(char *)NULL);
svn_memcache_t *memcache;
svn_membuffer_t *membuffer;
svn_boolean_t no_handler;
svn_boolean_t cache_txdeltas;
svn_boolean_t cache_fulltexts;
svn_boolean_t cache_revprops;
const char *cache_namespace;
/* Evaluating the cache configuration. */
SVN_ERR(read_config(&memcache,
&no_handler,
&cache_namespace,
&cache_txdeltas,
&cache_fulltexts,
&cache_revprops,
fs,
pool));
prefix = apr_pstrcat(pool, "ns:", cache_namespace, ":", prefix, NULL);
membuffer = svn_cache__get_global_membuffer_cache();
/* Make the cache for revision roots. For the vast majority of
* commands, this is only going to contain a few entries (svnadmin
* dump/verify is an exception here), so to reduce overhead let's
* try to keep it to just one page. I estimate each entry has about
* 72 bytes of overhead (svn_revnum_t key, svn_fs_id_t +
* id_private_t + 3 strings for value, and the cache_entry); the
* default pool size is 8192, so about a hundred should fit
* comfortably. */
SVN_ERR(create_cache(&(ffd->rev_root_id_cache),
NULL,
membuffer,
1, 100,
svn_fs_fs__serialize_id,
svn_fs_fs__deserialize_id,
sizeof(svn_revnum_t),
apr_pstrcat(pool, prefix, "RRI", (char *)NULL),
fs,
no_handler,
fs->pool));
/* Rough estimate: revision DAG nodes have size around 320 bytes, so
* let's put 16 on a page. */
SVN_ERR(create_cache(&(ffd->rev_node_cache),
NULL,
membuffer,
1024, 16,
svn_fs_fs__dag_serialize,
svn_fs_fs__dag_deserialize,
APR_HASH_KEY_STRING,
apr_pstrcat(pool, prefix, "DAG", (char *)NULL),
fs,
no_handler,
fs->pool));
/* 1st level DAG node cache */
ffd->dag_node_cache = svn_fs_fs__create_dag_cache(pool);
/* Very rough estimate: 1K per directory. */
SVN_ERR(create_cache(&(ffd->dir_cache),
NULL,
membuffer,
1024, 8,
svn_fs_fs__serialize_dir_entries,
svn_fs_fs__deserialize_dir_entries,
APR_HASH_KEY_STRING,
apr_pstrcat(pool, prefix, "DIR", (char *)NULL),
fs,
no_handler,
fs->pool));
/* Only 16 bytes per entry (a revision number + the corresponding offset).
Since we want ~8k pages, that means 512 entries per page. */
SVN_ERR(create_cache(&(ffd->packed_offset_cache),
NULL,
membuffer,
32, 1,
svn_fs_fs__serialize_manifest,
svn_fs_fs__deserialize_manifest,
sizeof(svn_revnum_t),
apr_pstrcat(pool, prefix, "PACK-MANIFEST",
(char *)NULL),
fs,
no_handler,
fs->pool));
/* initialize node revision cache, if caching has been enabled */
SVN_ERR(create_cache(&(ffd->node_revision_cache),
NULL,
membuffer,
0, 0, /* Do not use inprocess cache */
svn_fs_fs__serialize_node_revision,
svn_fs_fs__deserialize_node_revision,
sizeof(pair_cache_key_t),
apr_pstrcat(pool, prefix, "NODEREVS", (char *)NULL),
fs,
no_handler,
fs->pool));
/* initialize node change list cache, if caching has been enabled */
SVN_ERR(create_cache(&(ffd->changes_cache),
NULL,
membuffer,
0, 0, /* Do not use inprocess cache */
svn_fs_fs__serialize_changes,
svn_fs_fs__deserialize_changes,
sizeof(svn_revnum_t),
apr_pstrcat(pool, prefix, "CHANGES", (char *)NULL),
fs,
no_handler,
fs->pool));
/* if enabled, cache fulltext and other derived information */
if (cache_fulltexts)
{
SVN_ERR(create_cache(&(ffd->fulltext_cache),
memcache,
membuffer,
0, 0, /* Do not use inprocess cache */
/* Values are svn_stringbuf_t */
NULL, NULL,
sizeof(pair_cache_key_t),
apr_pstrcat(pool, prefix, "TEXT", (char *)NULL),
fs,
no_handler,
fs->pool));
SVN_ERR(create_cache(&(ffd->properties_cache),
NULL,
membuffer,
0, 0, /* Do not use inprocess cache */
svn_fs_fs__serialize_properties,
svn_fs_fs__deserialize_properties,
sizeof(pair_cache_key_t),
apr_pstrcat(pool, prefix, "PROP",
(char *)NULL),
fs,
no_handler,
fs->pool));
SVN_ERR(create_cache(&(ffd->mergeinfo_cache),
NULL,
membuffer,
0, 0, /* Do not use inprocess cache */
svn_fs_fs__serialize_mergeinfo,
svn_fs_fs__deserialize_mergeinfo,
APR_HASH_KEY_STRING,
apr_pstrcat(pool, prefix, "MERGEINFO",
(char *)NULL),
fs,
no_handler,
fs->pool));
SVN_ERR(create_cache(&(ffd->mergeinfo_existence_cache),
NULL,
membuffer,
0, 0, /* Do not use inprocess cache */
/* Values are svn_stringbuf_t */
NULL, NULL,
APR_HASH_KEY_STRING,
apr_pstrcat(pool, prefix, "HAS_MERGEINFO",
(char *)NULL),
fs,
no_handler,
fs->pool));
}
else
{
ffd->fulltext_cache = NULL;
ffd->properties_cache = NULL;
ffd->mergeinfo_cache = NULL;
ffd->mergeinfo_existence_cache = NULL;
}
/* initialize revprop cache, if full-text caching has been enabled */
if (cache_revprops)
{
SVN_ERR(create_cache(&(ffd->revprop_cache),
NULL,
membuffer,
0, 0, /* Do not use inprocess cache */
svn_fs_fs__serialize_properties,
svn_fs_fs__deserialize_properties,
sizeof(pair_cache_key_t),
apr_pstrcat(pool, prefix, "REVPROP",
(char *)NULL),
fs,
no_handler,
fs->pool));
}
else
{
ffd->revprop_cache = NULL;
}
/* if enabled, cache text deltas and their combinations */
if (cache_txdeltas)
{
SVN_ERR(create_cache(&(ffd->txdelta_window_cache),
NULL,
membuffer,
0, 0, /* Do not use inprocess cache */
svn_fs_fs__serialize_txdelta_window,
svn_fs_fs__deserialize_txdelta_window,
APR_HASH_KEY_STRING,
apr_pstrcat(pool, prefix, "TXDELTA_WINDOW",
(char *)NULL),
fs,
no_handler,
fs->pool));
SVN_ERR(create_cache(&(ffd->combined_window_cache),
NULL,
membuffer,
0, 0, /* Do not use inprocess cache */
/* Values are svn_stringbuf_t */
NULL, NULL,
APR_HASH_KEY_STRING,
apr_pstrcat(pool, prefix, "COMBINED_WINDOW",
(char *)NULL),
fs,
no_handler,
fs->pool));
}
else
{
ffd->txdelta_window_cache = NULL;
ffd->combined_window_cache = NULL;
}
return SVN_NO_ERROR;
}
/* Baton to be used for the remove_txn_cache() pool cleanup function, */
struct txn_cleanup_baton_t
{
/* the cache to reset */
svn_cache__t *txn_cache;
/* the position where to reset it */
svn_cache__t **to_reset;
};
/* APR pool cleanup handler that will reset the cache pointer given in
BATON_VOID. */
static apr_status_t
remove_txn_cache(void *baton_void)
{
struct txn_cleanup_baton_t *baton = baton_void;
/* be careful not to hurt performance by resetting newer txn's caches. */
if (*baton->to_reset == baton->txn_cache)
{
/* This is equivalent to calling svn_fs_fs__reset_txn_caches(). */
*baton->to_reset = NULL;
}
return APR_SUCCESS;
}
/* This function sets / registers the required callbacks for a given
* transaction-specific *CACHE object, if CACHE is not NULL and a no-op
* otherwise. In particular, it will ensure that *CACHE gets reset to NULL
* upon POOL destruction latest.
*/
static void
init_txn_callbacks(svn_cache__t **cache,
apr_pool_t *pool)
{
if (*cache != NULL)
{
struct txn_cleanup_baton_t *baton;
baton = apr_palloc(pool, sizeof(*baton));
baton->txn_cache = *cache;
baton->to_reset = cache;
apr_pool_cleanup_register(pool,
baton,
remove_txn_cache,
apr_pool_cleanup_null);
}
}
svn_error_t *
svn_fs_fs__initialize_txn_caches(svn_fs_t *fs,
const char *txn_id,
apr_pool_t *pool)
{
fs_fs_data_t *ffd = fs->fsap_data;
/* Transaction content needs to be carefully prefixed to virtually
eliminate any chance for conflicts. The (repo, txn_id) pair
should be unique but if a transaction fails, it might be possible
to start a new transaction later that receives the same id.
Therefore, throw in a uuid as well - just to be sure. */
const char *prefix = apr_pstrcat(pool,
"fsfs:", fs->uuid,
"/", fs->path,
":", txn_id,
":", svn_uuid_generate(pool), ":",
(char *)NULL);
/* We don't support caching for concurrent transactions in the SAME
* FSFS session. Maybe, you forgot to clean POOL. */
if (ffd->txn_dir_cache != NULL || ffd->concurrent_transactions)
{
ffd->txn_dir_cache = NULL;
ffd->concurrent_transactions = TRUE;
return SVN_NO_ERROR;
}
/* create a txn-local directory cache */
SVN_ERR(create_cache(&ffd->txn_dir_cache,
NULL,
svn_cache__get_global_membuffer_cache(),
1024, 8,
svn_fs_fs__serialize_dir_entries,
svn_fs_fs__deserialize_dir_entries,
APR_HASH_KEY_STRING,
apr_pstrcat(pool, prefix, "TXNDIR",
(char *)NULL),
fs,
TRUE,
pool));
/* reset the transaction-specific cache if the pool gets cleaned up. */
init_txn_callbacks(&(ffd->txn_dir_cache), pool);
return SVN_NO_ERROR;
}
void
svn_fs_fs__reset_txn_caches(svn_fs_t *fs)
{
/* we can always just reset the caches. This may degrade performance but
* can never cause in incorrect behavior. */
fs_fs_data_t *ffd = fs->fsap_data;
ffd->txn_dir_cache = NULL;
}
|