Skip to content

Commit b01a06e

Browse files
ldanilekConvex, Inc.
authored andcommitted
REPL upload source package (#26915)
instead of (in addition to, for now) storing the source code for REPL queries in `_module_versions`, store it in S3. also remove unnecessary Options GitOrigin-RevId: e6d6acb36e57d6556d08544491b67d6f7ae9915d
1 parent b230820 commit b01a06e

File tree

13 files changed

+56
-99
lines changed

13 files changed

+56
-99
lines changed

crates/application/src/application_function_runner/mod.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,9 +1272,7 @@ impl<RT: Runtime> ApplicationFunctionRunner<RT> {
12721272
source_maps.insert(module_path.module_path.clone(), source_map);
12731273
}
12741274

1275-
let source_package_id = module.source_package_id.ok_or_else(|| {
1276-
anyhow::anyhow!("Source package is required to execute actions")
1277-
})?;
1275+
let source_package_id = module.source_package_id;
12781276
let source_package = SourcePackageModel::new(&mut tx)
12791277
.get(source_package_id)
12801278
.await?
@@ -1674,7 +1672,7 @@ impl<RT: Runtime> ApplicationFunctionRunner<RT> {
16741672
&self,
16751673
udf_config: UdfConfig,
16761674
new_modules: Vec<ModuleConfig>,
1677-
source_package: Option<SourcePackage>,
1675+
source_package: SourcePackage,
16781676
) -> anyhow::Result<Result<BTreeMap<CanonicalizedModulePath, AnalyzedModule>, JsError>> {
16791677
// We use the latest environment variables at the time of the deployment
16801678
// this is not transactional with the rest of the deploy.
@@ -1715,9 +1713,6 @@ impl<RT: Runtime> ApplicationFunctionRunner<RT> {
17151713
source_maps.insert(path.clone(), source_map);
17161714
}
17171715
}
1718-
let source_package = source_package.ok_or_else(|| {
1719-
anyhow::anyhow!("Source package is required to analyze action modules")
1720-
})?;
17211716

17221717
// Fetch source and external_deps presigned URI first
17231718
let source_uri_future = self

crates/application/src/lib.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ use model::{
218218
source_packages::{
219219
types::SourcePackage,
220220
upload_download::upload_package,
221+
SourcePackageModel,
221222
},
222223
udf_config::{
223224
types::UdfConfig,
@@ -340,7 +341,7 @@ pub struct ApplyConfigArgs {
340341
pub schema_id: Option<String>,
341342
pub modules: Vec<ModuleConfig>,
342343
pub udf_config: UdfConfig,
343-
pub source_package: Option<SourcePackage>,
344+
pub source_package: SourcePackage,
344345
pub analyze_results: BTreeMap<CanonicalizedModulePath, AnalyzedModule>,
345346
}
346347

@@ -1526,7 +1527,7 @@ impl<RT: Runtime> Application<RT> {
15261527
&self,
15271528
udf_config: UdfConfig,
15281529
new_modules: Vec<ModuleConfig>,
1529-
source_package: Option<SourcePackage>,
1530+
source_package: SourcePackage,
15301531
) -> anyhow::Result<Result<BTreeMap<CanonicalizedModulePath, AnalyzedModule>, JsError>> {
15311532
self.runner
15321533
.analyze(udf_config, new_modules, source_package)
@@ -1746,7 +1747,7 @@ impl<RT: Runtime> Application<RT> {
17461747
config_metadata.clone(),
17471748
modules,
17481749
udf_config,
1749-
source_package,
1750+
Some(source_package),
17501751
analyze_results,
17511752
schema_id,
17521753
)
@@ -1837,7 +1838,7 @@ impl<RT: Runtime> Application<RT> {
18371838
&self,
18381839
modules: &Vec<ModuleConfig>,
18391840
external_deps_id_and_pkg: Option<(ExternalDepsPackageId, ExternalDepsPackage)>,
1840-
) -> anyhow::Result<Option<SourcePackage>> {
1841+
) -> anyhow::Result<SourcePackage> {
18411842
// If there are any node actions, turn on the lambdas.
18421843
if modules
18431844
.iter()
@@ -1879,12 +1880,12 @@ impl<RT: Runtime> Application<RT> {
18791880
tracing::info!("Source package size: {}", package_size);
18801881
log_source_package_size_bytes_total(package_size);
18811882

1882-
Ok(Some(SourcePackage {
1883+
Ok(SourcePackage {
18831884
storage_key,
18841885
sha256,
18851886
external_deps_package_id,
18861887
package_size,
1887-
}))
1888+
})
18881889
}
18891890

18901891
// Clear all records for specified tables concurrently, potentially taking
@@ -1915,6 +1916,11 @@ impl<RT: Runtime> Application<RT> {
19151916
)
19161917
.await?;
19171918

1919+
// Write (and commit) the module source to S3.
1920+
// This will become a dangling reference since the _modules entry won't
1921+
// be committed to the database, but we have to deal with those anyway.
1922+
let source_package = self.upload_package(&vec![module.clone()], None).await?;
1923+
19181924
let mut tx = self.begin(identity.clone()).await?;
19191925

19201926
// Use the last pushed version. If there hasn't been a push
@@ -1936,7 +1942,11 @@ impl<RT: Runtime> Application<RT> {
19361942
};
19371943

19381944
let analyze_results = self
1939-
.analyze(udf_config.clone(), vec![module.clone()], None)
1945+
.analyze(
1946+
udf_config.clone(),
1947+
vec![module.clone()],
1948+
source_package.clone(),
1949+
)
19401950
.await?
19411951
.map_err(|js_error| {
19421952
let metadata = ErrorMetadata::bad_request(
@@ -1963,16 +1973,20 @@ impl<RT: Runtime> Application<RT> {
19631973
}
19641974
let analyzed_function = analyzed_function.context("Missing default export.")?;
19651975

1976+
let source_package_id = SourcePackageModel::new(&mut tx).put(source_package).await?;
1977+
19661978
// 3. Add the module
19671979
ModuleModel::new(&mut tx)
1968-
.put_standalone(
1980+
.put(
19691981
CanonicalizedComponentModulePath {
19701982
component: ComponentId::Root,
19711983
module_path: module_path.clone(),
19721984
},
19731985
module.source,
1986+
source_package_id,
19741987
module.source_map,
1975-
analyzed_module,
1988+
Some(analyzed_module),
1989+
ModuleEnvironment::Isolate,
19761990
)
19771991
.await?;
19781992

crates/application/src/test_helpers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ impl<RT: Runtime> Application<RT> {
332332
ConfigMetadata::new(),
333333
test_source.clone(),
334334
udf_config,
335-
source_package,
335+
Some(source_package),
336336
analyze_results,
337337
Some(schema_id),
338338
)

crates/application/src/tests/source_package.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::collections::BTreeMap;
22

3-
use anyhow::Context;
43
use common::{
54
components::ComponentId,
65
runtime::Runtime,
@@ -49,10 +48,7 @@ async fn test_source_package(rt: ProdRuntime) -> anyhow::Result<()> {
4948
storage_key,
5049
sha256,
5150
..
52-
} = application
53-
.upload_package(&package, None)
54-
.await?
55-
.context("With functions should upload")?;
51+
} = application.upload_package(&package, None).await?;
5652

5753
let result =
5854
download_package(application.modules_storage().clone(), storage_key, sha256).await?;

crates/isolate/src/environment/helpers/module_loader.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ use std::{
33
sync::Arc,
44
};
55

6-
use anyhow::{
7-
anyhow,
8-
Context,
9-
};
6+
use anyhow::anyhow;
107
use common::{
118
components::ComponentId,
129
document::ParsedDocument,
@@ -93,12 +90,10 @@ async fn download_module_source_from_package<RT: Runtime>(
9390
tx: &mut Transaction<RT>,
9491
modules_storage: Arc<dyn Storage>,
9592
modules_tablet: TabletId,
96-
source_package_id: Option<SourcePackageId>,
93+
source_package_id: SourcePackageId,
9794
) -> anyhow::Result<HashMap<(ResolvedDocumentId, ModuleVersion), FullModuleSource>> {
9895
let mut result = HashMap::new();
99-
let source_package = SourcePackageModel::new(tx)
100-
.get(source_package_id.context("source package missing")?)
101-
.await?;
96+
let source_package = SourcePackageModel::new(tx).get(source_package_id).await?;
10297
let mut package = download_package(
10398
modules_storage,
10499
source_package.storage_key.clone(),

crates/isolate/src/tests/internal.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use keybroker::{
2121
};
2222
use model::{
2323
config::ConfigModel,
24+
source_packages::SourcePackageModel,
2425
udf_config::types::UdfConfig,
2526
};
2627
use must_let::must_let;
@@ -65,6 +66,11 @@ async fn test_udf_visibility(rt: TestRuntime) -> anyhow::Result<()> {
6566
.isolate
6667
.analyze(udf_config, modules_by_path, BTreeMap::new())
6768
.await??;
69+
70+
let source_package = SourcePackageModel::new(&mut tx)
71+
.get_latest()
72+
.await?
73+
.unwrap();
6874
drop(tx);
6975

7076
// Newer version + analyze results
@@ -74,7 +80,7 @@ async fn test_udf_visibility(rt: TestRuntime) -> anyhow::Result<()> {
7480
config_metadata.clone(),
7581
module_configs.clone(),
7682
UdfConfig::new_for_test(&t.rt, post_internal_npm_version.clone()),
77-
None,
83+
Some(source_package.into_value()),
7884
analyze_results.clone(),
7985
None,
8086
)

crates/local_backend/src/deploy_config.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -439,11 +439,7 @@ pub async fn push_config_handler(
439439
.await?;
440440
let end_upload_source_package = Instant::now();
441441
// Verify that we have not exceeded the max zipped or unzipped file size
442-
let combined_pkg_size = source_package
443-
.as_ref()
444-
.map(|pkg| pkg.package_size)
445-
.unwrap_or(PackageSize::default())
446-
+ external_deps_pkg_size;
442+
let combined_pkg_size = source_package.package_size + external_deps_pkg_size;
447443
combined_pkg_size.verify_size()?;
448444

449445
let udf_config = UdfConfig {
@@ -506,7 +502,7 @@ pub async fn analyze_modules_with_auth_config(
506502
application: &Application<ProdRuntime>,
507503
udf_config: UdfConfig,
508504
modules: Vec<ModuleConfig>,
509-
source_package: Option<SourcePackage>,
505+
source_package: SourcePackage,
510506
) -> anyhow::Result<(
511507
Option<ModuleConfig>,
512508
BTreeMap<CanonicalizedModulePath, AnalyzedModule>,
@@ -536,7 +532,7 @@ pub async fn analyze_modules(
536532
application: &Application<ProdRuntime>,
537533
udf_config: UdfConfig,
538534
modules: Vec<ModuleConfig>,
539-
source_package: Option<SourcePackage>,
535+
source_package: SourcePackage,
540536
) -> anyhow::Result<BTreeMap<CanonicalizedModulePath, AnalyzedModule>> {
541537
let num_dep_modules = modules.iter().filter(|m| m.path.is_deps()).count();
542538
anyhow::ensure!(

crates/local_backend/src/deploy_config2.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,7 @@ pub async fn start_push_handler(
396396
let app_pkg = st
397397
.application
398398
.upload_package(&app_modules, external_deps_id_and_pkg.clone())
399-
.await?
400-
.context("No package for app?")?;
399+
.await?;
401400
total_size += app_pkg.package_size;
402401
component_definition_packages.insert(ComponentDefinitionPath::root(), app_pkg);
403402

@@ -406,8 +405,7 @@ pub async fn start_push_handler(
406405
let component_pkg = st
407406
.application
408407
.upload_package(&component_modules, None)
409-
.await?
410-
.context("No package for component?")?;
408+
.await?;
411409
total_size += component_pkg.package_size;
412410
anyhow::ensure!(component_definition_packages
413411
.insert(component_def.definition_path.clone(), component_pkg)
@@ -423,7 +421,7 @@ pub async fn start_push_handler(
423421
&st.application,
424422
config.udf_config.clone(),
425423
config.app_definition.functions.clone(),
426-
Some(app_pkg.clone()),
424+
app_pkg.clone(),
427425
)
428426
.await?;
429427

@@ -475,7 +473,7 @@ pub async fn start_push_handler(
475473
&st.application,
476474
config.udf_config.clone(),
477475
component_def.functions.clone(),
478-
Some(component_pkg.clone()),
476+
component_pkg.clone(),
479477
)
480478
.await?;
481479
anyhow::ensure!(component_analysis_by_def_path

crates/model/src/config/module_loader.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ pub mod test_module_loader {
7474
tx: &mut Transaction<RT>,
7575
module_metadata: ParsedDocument<ModuleMetadata>,
7676
) -> anyhow::Result<Arc<FullModuleSource>> {
77-
let source_package_id = module_metadata
78-
.source_package_id
79-
.with_context(|| format!("Missing source package {}", module_metadata.id()))?;
77+
let source_package_id = module_metadata.source_package_id;
8078
let source_package = SourcePackageModel::new(tx)
8179
.get(source_package_id)
8280
.await?

crates/model/src/modules/mod.rs

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ impl<'a, RT: Runtime> ModuleModel<'a, RT> {
229229
module_path: path.clone(),
230230
},
231231
module.source,
232-
source_package_id,
232+
source_package_id.context("missing source_package_id")?,
233233
module.source_map,
234234
analyze_result,
235235
module.environment,
@@ -390,46 +390,12 @@ impl<'a, RT: Runtime> ModuleModel<'a, RT> {
390390
Ok(Some(module_metadata))
391391
}
392392

393-
/// Write a isolate-environment module to _module_versions, without source
394-
/// package.
395-
///
396-
/// This transaction must never be committed.
397-
/// All future reads of modules on this transaction will read from the
398-
/// database instead of the source package.
399-
pub async fn put_standalone(
400-
&mut self,
401-
path: CanonicalizedComponentModulePath,
402-
source: ModuleSource,
403-
source_map: Option<SourceMap>,
404-
analyze_result: AnalyzedModule,
405-
) -> anyhow::Result<()> {
406-
if !(self.tx.identity().is_admin() || self.tx.identity().is_system()) {
407-
anyhow::bail!(unauthorized_error("put_standalone_module"));
408-
}
409-
if path.module_path.is_system() {
410-
anyhow::bail!("You cannot push a function under '_system/'");
411-
}
412-
let component = path.component;
413-
let sha256 = hash_module_source(&source, source_map.as_ref());
414-
let (module_id, version) = self
415-
.put_module_metadata(
416-
path,
417-
None,
418-
Some(analyze_result),
419-
ModuleEnvironment::Isolate,
420-
sha256,
421-
)
422-
.await?;
423-
self.put_module_source_into_db(module_id, version, source, source_map, component)
424-
.await
425-
}
426-
427393
/// Put a module's source at a given path.
428394
pub async fn put(
429395
&mut self,
430396
path: CanonicalizedComponentModulePath,
431397
source: ModuleSource,
432-
source_package_id: Option<SourcePackageId>,
398+
source_package_id: SourcePackageId,
433399
source_map: Option<SourceMap>,
434400
analyze_result: Option<AnalyzedModule>,
435401
environment: ModuleEnvironment,
@@ -456,7 +422,7 @@ impl<'a, RT: Runtime> ModuleModel<'a, RT> {
456422
async fn put_module_metadata(
457423
&mut self,
458424
path: CanonicalizedComponentModulePath,
459-
source_package_id: Option<SourcePackageId>,
425+
source_package_id: SourcePackageId,
460426
analyze_result: Option<AnalyzedModule>,
461427
environment: ModuleEnvironment,
462428
sha256: Sha256Digest,

0 commit comments

Comments
 (0)