-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[kbn/optimizer] implement more efficient auto transpilation for node #79052
Merged
spalger
merged 4 commits into
elastic:master
from
spalger:implement/kbn-optimizer-auto-transpile
Oct 3, 2020
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6079712
[kbn/optimizer] implement more efficient auto transpilation for node
spalger 60dde93
Merge branch 'master' of github.com:elastic/kibana into implement/kbn…
spalger 939047b
Merge branch 'master' of github.com:elastic/kibana into implement/kbn…
spalger 2504b82
split up cache based on upstream branch
spalger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -208,6 +208,30 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
--- | ||
MIT License | ||
|
||
Copyright (c) 2014-present Sebastian McKenzie and other contributors | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
--- | ||
This product bundles [email protected] which is available under a | ||
"MIT" license. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. 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. | ||
*/ | ||
|
||
import Path from 'path'; | ||
|
||
// @ts-expect-error no types available | ||
import * as LmdbStore from 'lmdb-store'; | ||
import { REPO_ROOT } from '@kbn/dev-utils'; | ||
|
||
const CACHE_DIR = Path.resolve(REPO_ROOT, 'data/node_auto_transpilation_cache'); | ||
const reportError = () => { | ||
// right now I'm not sure we need to worry about errors, the cache isn't actually | ||
// necessary, and if the cache is broken it should just rebuild on the next restart | ||
// of the process. We don't know how often errors occur though and what types of | ||
// things might fail on different machines so we probably want some way to signal | ||
// to users that something is wrong | ||
}; | ||
|
||
const GLOBAL_ATIME = `${Date.now()}`; | ||
const MINUTE = 1000 * 60; | ||
const HOUR = MINUTE * 60; | ||
const DAY = HOUR * 24; | ||
|
||
interface Lmdb<T> { | ||
get(key: string): T | undefined; | ||
put(key: string, value: T, version?: number, ifVersion?: number): Promise<boolean>; | ||
remove(key: string, ifVersion?: number): Promise<boolean>; | ||
openDB<T>(options: { name: string; encoding: 'msgpack' | 'string' | 'json' | 'binary' }): Lmdb<T>; | ||
getRange(options?: { | ||
start?: T; | ||
end?: T; | ||
reverse?: boolean; | ||
limit?: number; | ||
versions?: boolean; | ||
}): Iterable<{ key: string; value: T }>; | ||
} | ||
|
||
export class Cache { | ||
private readonly codes: Lmdb<string>; | ||
private readonly atimes: Lmdb<string>; | ||
private readonly mtimes: Lmdb<string>; | ||
private readonly sourceMaps: Lmdb<any>; | ||
private readonly prefix: string; | ||
|
||
constructor(config: { prefix: string }) { | ||
this.prefix = config.prefix; | ||
|
||
this.codes = LmdbStore.open({ | ||
name: 'codes', | ||
path: CACHE_DIR, | ||
}); | ||
|
||
this.atimes = this.codes.openDB({ | ||
name: 'atimes', | ||
encoding: 'string', | ||
}); | ||
|
||
this.mtimes = this.codes.openDB({ | ||
name: 'mtimes', | ||
encoding: 'string', | ||
}); | ||
|
||
this.sourceMaps = this.codes.openDB({ | ||
name: 'sourceMaps', | ||
encoding: 'msgpack', | ||
}); | ||
|
||
// after the process has been running for 30 minutes prune the | ||
// keys which haven't been used in 30 days. We use `unref()` to | ||
// make sure this timer doesn't hold other processes open | ||
// unexpectedly | ||
setTimeout(() => { | ||
this.pruneOldKeys(); | ||
}, 30 * MINUTE).unref(); | ||
} | ||
|
||
getMtime(path: string) { | ||
return this.mtimes.get(this.getKey(path)); | ||
} | ||
|
||
getCode(path: string) { | ||
const key = this.getKey(path); | ||
|
||
// when we use a file from the cache set the "atime" of that cache entry | ||
// so that we know which cache items we use and which haven't been | ||
// touched in a long time (currently 30 days) | ||
this.atimes.put(key, GLOBAL_ATIME).catch(reportError); | ||
|
||
return this.codes.get(key); | ||
} | ||
|
||
getSourceMap(path: string) { | ||
return this.sourceMaps.get(this.getKey(path)); | ||
} | ||
|
||
update(path: string, file: { mtime: string; code: string; map: any }) { | ||
const key = this.getKey(path); | ||
|
||
Promise.all([ | ||
this.atimes.put(key, GLOBAL_ATIME), | ||
this.mtimes.put(key, file.mtime), | ||
this.codes.put(key, file.code), | ||
this.sourceMaps.put(key, file.map), | ||
]).catch(reportError); | ||
} | ||
|
||
private getKey(path: string) { | ||
return `${this.prefix}${path}`; | ||
} | ||
|
||
private async pruneOldKeys() { | ||
try { | ||
const ATIME_LIMIT = Date.now() - 30 * DAY; | ||
const BATCH_SIZE = 1000; | ||
|
||
const validKeys: string[] = []; | ||
const invalidKeys: string[] = []; | ||
|
||
for (const { key, value } of this.atimes.getRange()) { | ||
const atime = parseInt(value, 10); | ||
if (atime < ATIME_LIMIT) { | ||
invalidKeys.push(key); | ||
} else { | ||
validKeys.push(key); | ||
} | ||
|
||
if (validKeys.length + invalidKeys.length >= BATCH_SIZE) { | ||
const promises = new Set(); | ||
|
||
if (invalidKeys.length) { | ||
for (const k of invalidKeys) { | ||
// all these promises are the same currently, so Set() will | ||
// optimise this to a single promise, but I wouldn't be shocked | ||
// if a future version starts returning independent promises so | ||
// this is just for some future-proofing | ||
promises.add(this.atimes.remove(k)); | ||
promises.add(this.mtimes.remove(k)); | ||
promises.add(this.codes.remove(k)); | ||
promises.add(this.sourceMaps.remove(k)); | ||
} | ||
} else { | ||
// delay a smidge to allow other things to happen before the next batch of checks | ||
promises.add(new Promise((resolve) => setTimeout(resolve, 1))); | ||
} | ||
|
||
invalidKeys.length = 0; | ||
validKeys.length = 0; | ||
await Promise.all(Array.from(promises)); | ||
} | ||
} | ||
} catch { | ||
// ignore errors, the cache is totally disposable and will rebuild if there is some sort of corruption | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@spalger why don't we create a new package called
@kbn/node-env
or just@kbn/node
in order to store that logic plus the one we have onsrc/setup_node_env
? Do you think that will be to much and we should just add it to thekbn/optimizer
extending its boundaries?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be too little to justify a new package personally, and it fits into what I had imagined
@kbn/optimizer
would become. I also plan to move the rest of setup_node_env into@kbn/optimizer/node
but just taking the simplest path for now.