Commit graph

3594 commits

Author SHA1 Message Date
Ankush Menat
2a5c778235
perf: speedup date parsing (#29090) 2025-01-09 06:55:13 +00:00
Ankush Menat
fdba41c682
perf: misc client cache improvements (#29070)
* perf: Reduce penalty for lack of redis connection

If redis isn't running than this client cache is slower than default
implementation because of the extra locking overhead.

* test: update perf redis counts

* perf: cache table columns in client-cache

* fix: race condition on cache-client_cache init

Rare but apparant in synthetic benchmarks.

Cache is set but client cache is still being initialized then request
will fail.

* perf: Don't run notifications when loading document

WHAT?

* fix: use cached doc to repopulate

* perf: reduce get_meta calls
2025-01-07 16:14:43 +05:30
Ankush Menat
b9c0662de6 fix: Make client cache work without redis too 2025-01-07 13:46:47 +05:30
Ankush Menat
06816f2817 perf: client-cache for defaults 2025-01-07 12:51:10 +05:30
Ankush Menat
33bd1bc09b fix: Flushall shouldn't attempt reading keys 2025-01-07 11:56:22 +05:30
Ankush Menat
b5e2569c0c feat: statistics for cache performance
These can be used for logging or debugging the behaviour of the cache.
2025-01-07 11:47:52 +05:30
Ankush Menat
8dd4ad53e2 fix: data race between GET and INVALIDATE
Fix for the documented example given here: https://redis.io/docs/latest/develop/reference/client-side-caching/#avoiding-race-conditions
2025-01-07 11:09:42 +05:30
Ankush Menat
c14d416d19 docs: add usage / notes in code 2025-01-06 19:28:37 +05:30
Ankush Menat
15f5adc25a refactor: use namedtuple for readability 2025-01-06 19:28:33 +05:30
Ankush Menat
b51cfc1705 perf: minimize penalty for broken client cache 2025-01-06 18:57:57 +05:30
Ankush Menat
119af71ae3 refactor: variable names, force RESP2 2025-01-06 18:57:57 +05:30
Ankush Menat
98b1df7dac fix: guard all writes with an RLock 2025-01-06 18:57:57 +05:30
Ankush Menat
53f085e0f4 test: ttl and maxsize 2025-01-06 18:57:57 +05:30
Ankush Menat
55ae5615d0 fix: clear all meta cache only when doctype is not specified 2025-01-06 18:57:57 +05:30
Ankush Menat
f332852415 fix: start tracking keys set by us immediately 2025-01-06 18:57:57 +05:30
Ankush Menat
f74ada155e fix: Don't serve local cache values when it becomes unhealthy 2025-01-06 18:57:57 +05:30
Ankush Menat
45d414fe29 refactor: per-key TTL 2025-01-06 18:57:57 +05:30
Ankush Menat
21f0cda732 refactor: Reuse original cache connection
Drops redis connection per worker from 3 to 2!

Nothing wrong with doing this.
2025-01-06 18:57:57 +05:30
Ankush Menat
01cfa647a2 fix: set/del locally too
This is required if we do NOLOOP
2025-01-06 18:57:57 +05:30
Ankush Menat
e7139a1395 perf: store meta in client cache 2025-01-06 18:57:57 +05:30
Ankush Menat
cd47bee65e fix: Avoid data races by accessing request specific cache
Imagine:
| client 1 | client 2 |
| ---      | ---      |
| rd(x)    |          |
|          |  wr(x)   |
| inv(x)   |          |
| rd(x)    |          |  <- This will end up using request specific cache!
2025-01-06 18:57:57 +05:30
Ankush Menat
764c3134cb fix: expire client cache every 10 minutes
We can be more aggressive later, this is a good starting parting.
2025-01-06 18:57:57 +05:30
Ankush Menat
796e51df62 fix: limit # of keys by maxsize
Picking 1024 keys assuming 4kb avg size. In practice most things are
smaller so 1024 should be good enough!

This will likely only affect multitenant deploy with many sites.
2025-01-06 18:57:57 +05:30
Ankush Menat
590e7ff185 fix: Ensure that RESP3 is not in use
One can set it in conf and it won't work as expected because pubsub
format is different
2025-01-06 18:57:57 +05:30
Ankush Menat
67848aa920 fix: handle temporary disconnects 2025-01-06 18:57:57 +05:30
Ankush Menat
e21a7112e9 fix: flushall handler
Also no need to check channel names?
2025-01-06 18:57:57 +05:30
Ankush Menat
b96b8c815d feat: Initate client-side caching 2025-01-06 18:57:57 +05:30
Ankush Menat
98cbe05c9a perf: use cached function signature 2025-01-06 11:23:30 +05:30
Ankush Menat
28aafcd708 perf: Don't validate types if there are no input types
Many functions just have return type annotated, in such cases running
validation code is not necessary.
2025-01-06 11:20:37 +05:30
Ankush Menat
dd6346e466
perf: Avoid parsing same field repeatedly (#29030) 2025-01-02 15:51:16 +00:00
Ankush Menat
bd3af4391f
fix: Commit ID parsing (#28975)
This is/was never returning anything.

- Arguments come before revs
- `cd` is kinda unnecessary
2024-12-31 10:33:57 +00:00
Ankush Menat
3788f30b5b
perf: reduce RQ Worker's polling frequency (#28957) 2024-12-30 14:37:00 +05:30
Ankush Menat
766cb64d55
perf!: Cache site configs in memory for 60 seconds (#28869)
This is middle ground between caching it completely and requiring a
restart/signal to reload vs always reloading it.

I don't know any use cases that can break from this, nowhere in code
configs should be expected to reload instantly.

This change is only applied to requests for now
2024-12-27 16:21:14 +00:00
Ankush Menat
28453f59f8
refactor: Flatten @site_cache's internal representation (#28939)
This makes maxsize deterministic while estimating memory costs of
using this function.

E.g. If I want to cache site config and I'd prefer to keep 16 recent
site configs in memory, there's no way to handle this. Site specific
maxsize means if I have 1000 sites on bench I'll have 1000 keys in
cache.

This change makes behaviour similar to lru_cache which is how I thought
it workerd TBH.
2024-12-27 15:50:07 +00:00
Ankush Menat
416919e880
Revert "feat: Use RESP3 for Redis cache connections (#28929)" (#28938)
This reverts commit 6f7b4c412e.
2024-12-27 13:19:35 +00:00
Ankush Menat
6f7b4c412e
feat: Use RESP3 for Redis cache connections (#28929)
RESP3 has PUSH support which is useful for implementing client side
caching. Enabling this before I work on that to test if anything breaks
with this.

No need to do this for background jobs instance just yet, infrequent
accesses and performance doesn't matter as much.
2024-12-27 06:15:59 +00:00
Akhil Narang
7af83f6d37
Merge remote-tracking branch 'upstream/develop' into invalid-encr-key-message
* upstream/develop: (1373 commits)
  perf: cache dynamic links map in Redis (#28878)
  fix: Never query `flag_print_sql` in `developer_mode=0` (#28884)
  fix(restore): remove MariaDB view security definers
  fix: sanitize user input during setup wizard
  feat(sanitize_column): improve check
  refactor: make optimizations.py private entirely (#28872)
  fix(site_cache): site cache thread safety (#28870)
  chore(printview): change error message
  perf: speedup `frappe.call` by ~8x (#28866)
  test: reduce noise in test output (#28862)
  chore: spelling_invalid_values (#28858)
  fix: Remove misleading os.O_NONBLOCK flag (#28859)
  fix: string replacement in error logger
  perf(gthread): Pin web workers to a single core (#28854)
  fix: MariaDBDatabase.get_tables() should not query the entire database schema (#28846)
  fix: add strings and fields to translation
  fix: typo in test controller boilerplate
  perf: faster add_to_date (#28843)
  perf(version): Make get_versions fast for autoincrement doctypes (#28847)
  refactor: log in monitor as well
  ...
2024-12-24 12:36:33 +05:30
Akhil Narang
6b9960ca5c
chore: update message
Signed-off-by: Akhil Narang <me@akhilnarang.dev>
2024-12-24 12:35:19 +05:30
Akhil Narang
105a3b153d
Merge pull request #28513 from akhilnarang/fix-retry-background-job-after-job-hook
fix(background_jobs): init site if required for after_job hooks
2024-12-24 11:38:32 +05:30
Akhil Narang
b5bad56cdd
feat(sanitize_column): improve check
Signed-off-by: Akhil Narang <me@akhilnarang.dev>
2024-12-23 16:16:08 +05:30
Akhil Narang
21a6d2a717
Merge pull request #28868 from akhilnarang/printview-cleanup-checks
chore(printview): change error message
2024-12-23 15:27:54 +05:30
Ankush Menat
17686eba3b
fix(site_cache): site cache thread safety (#28870)
Identified two cases where site cache can break:

1. Other thread clears cache using clear_cache because of TTL or manual
   eviction.
2. Other thread pops the eliment we are about to read because of
   `maxsize` limit.

This change should fix both and even make it lil bit faster.
2024-12-23 13:44:19 +05:30
Akhil Narang
5a4239fbe3
chore(printview): change error message
Signed-off-by: Akhil Narang <me@akhilnarang.dev>
2024-12-23 13:29:44 +05:30
Ankush Menat
f243aa1942
perf: faster add_to_date (#28843)
* fix(DX): Accept None directly as input to add_to_date

It's already supported, signature is just not updated.

* perf: use efficient date parser
2024-12-21 05:23:34 +00:00
Ankush Menat
9e9096834f
perf: pretty_date - avoid useless dt->string->dt cycle (#28842) 2024-12-19 14:53:38 +00:00
Ankush Menat
17cc356915
perf: speed up flt by 1.06x and get_system_settings by 1.32x (#28841)
* perf: resolve rounding method once

When rounding method is explcitly specified it's 1.4x faster.

* perf: reorder checks

Bankers rounding is default and most common now

* perf: speedup get_system_settings
2024-12-19 14:38:45 +00:00
Ankush Menat
1c2f8abb4e
perf: speedup get_datetime by ~9.5x (#28840) 2024-12-19 20:01:45 +05:30
Ankush Menat
9419344c76
fix: always print tracebacks (#28838)
* fix: fallback for always printing tracebacks

I don't recall ever hitting "no" to this prompt. It's of no use for me.

Also, this makes automated scripts not really automated.

* revert: prompting for exceptions

Always print full exception
2024-12-19 12:46:11 +00:00
Ankush Menat
9e8ab92371
refactor: move all optimizations and pre/post fork hooks to separate file (#28832)
Now they will truly execute before/after fork = :pinch: few bytes saved!
2024-12-19 16:46:26 +05:30
Ankush Menat
7dd15e3613
perf: speedup pickling of document objects (#28823)
* perf: Use latest pickle protocol

* perf: pop flags from cached documents

This is also the right thing to do, things like `doc.flags.for_update`
shouldn't be "cached".
2024-12-18 10:18:04 +00:00