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

Merge master to Java21 branch #43025

Merged
merged 17 commits into from
Jul 2, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ private void stopObservation(ObserverContext observerContext) {
"Total response response time for all requests", tags)).increment(duration);
metricRegistry.counter(new MetricId("requests_total",
"Total number of requests", tags)).increment();
if (statusCode != null && 400 <= statusCode && statusCode < 600) {
metricRegistry.counter(new MetricId("response_errors_total",
"Total number of response errors", tags)).increment();
}
} catch (RuntimeException e) {
handleError("multiple metrics", tags, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.ballerina.projects.internal.DependencyManifestBuilder;
import io.ballerina.projects.internal.ManifestBuilder;
import io.ballerina.projects.internal.model.CompilerPluginDescriptor;
import io.ballerina.projects.util.ProjectUtils;
import io.ballerina.tools.diagnostics.Diagnostic;
import org.ballerinalang.model.elements.PackageID;
import org.wso2.ballerinalang.compiler.PackageCache;
Expand Down Expand Up @@ -624,13 +625,17 @@ private Map<ModuleId, ModuleContext> copyModules(Package oldPackage) {
}

private Package createNewPackage() {
Package oldPackage = this.project.currentPackage();
PackageResolution oldResolution = oldPackage.getResolution();;
PackageContext newPackageContext = new PackageContext(this.project, this.packageId, this.packageManifest,
this.dependencyManifest, this.ballerinaTomlContext, this.dependenciesTomlContext,
this.cloudTomlContext, this.compilerPluginTomlContext, this.balToolTomlContext,
this.packageMdContext, this.compilationOptions, this.moduleContextMap,
DependencyGraph.emptyGraph());
this.project.setCurrentPackage(new Package(newPackageContext, this.project));

if (isOldDependencyGraphValid(oldPackage, this.project.currentPackage())) {
this.project.currentPackage().packageContext().getResolution(oldResolution);
}
CompilationOptions offlineCompOptions = CompilationOptions.builder().setOffline(true).build();
offlineCompOptions = offlineCompOptions.acceptTheirs(project.currentPackage().compilationOptions());
DependencyGraph<ResolvedPackageDependency> newDepGraph = this.project.currentPackage().packageContext()
Expand All @@ -639,6 +644,22 @@ private Package createNewPackage() {
return this.project.currentPackage();
}

private static boolean isOldDependencyGraphValid(Package oldPackage, Package currentPackage) {
Set<String> oldPackageImports = ProjectUtils.getPackageImports(oldPackage);
Set<String> currentPackageImports = ProjectUtils.getPackageImports(currentPackage);
String oldDependencyTomlContent = oldPackage.packageContext.dependenciesTomlContext()
.map(d -> d.tomlDocument().textDocument().toString()).orElse("");
String currentDependencyTomlContent = currentPackage.packageContext.dependenciesTomlContext()
.map(d -> d.tomlDocument().textDocument().toString()).orElse("");
String oldBallerinaTomlContent = oldPackage.packageContext.ballerinaTomlContext()
.map(d -> d.tomlDocument().textDocument().toString()).orElse("");
String currentBallerinaTomlContent = currentPackage.packageContext.ballerinaTomlContext()
.map(d -> d.tomlDocument().textDocument().toString()).orElse("");
return oldPackageImports.equals(currentPackageImports) &&
oldDependencyTomlContent.equals(currentDependencyTomlContent) &&
oldBallerinaTomlContent.equals(currentBallerinaTomlContent);
}

private void cleanPackageCache(DependencyGraph<ResolvedPackageDependency> oldGraph,
DependencyGraph<ResolvedPackageDependency> newGraph) {
io.ballerina.projects.environment.PackageCache environmentPackageCache =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,13 @@ BuildToolResolution getBuildToolResolution() {
return buildToolResolution;
}

PackageResolution getResolution(PackageResolution oldResolution) {
if (packageResolution == null) {
packageResolution = PackageResolution.from(oldResolution, this, this.compilationOptions);
}
return packageResolution;
}

Collection<PackageDependency> packageDependencies() {
return packageDependencies;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,67 @@ private PackageResolution(PackageContext rootPackageContext, CompilationOptions
this.blendedManifest = createBlendedManifest(rootPackageContext, projectEnvContext,
this.resolutionOptions.offline());
diagnosticList.addAll(this.blendedManifest.diagnosticResult().allDiagnostics);

this.moduleResolver = createModuleResolver(rootPackageContext, projectEnvContext);
this.dependencyGraph = buildDependencyGraph();
DependencyResolution dependencyResolution = new DependencyResolution(
projectEnvContext.getService(PackageCache.class), moduleResolver, dependencyGraph);
resolveDependencies(dependencyResolution);
}

private PackageResolution(PackageResolution packageResolution, PackageContext rootPackageContext,
CompilationOptions compilationOptions) {
this.rootPackageContext = rootPackageContext;
this.diagnosticList = new ArrayList<>();
this.compilationOptions = compilationOptions;
this.resolutionOptions = getResolutionOptions(rootPackageContext, compilationOptions);
ProjectEnvironment projectEnvContext = rootPackageContext.project().projectEnvironmentContext();
this.packageResolver = projectEnvContext.getService(PackageResolver.class);
this.blendedManifest = createBlendedManifest(rootPackageContext, projectEnvContext,
this.resolutionOptions.offline());
diagnosticList.addAll(this.blendedManifest.diagnosticResult().allDiagnostics);
this.moduleResolver = createModuleResolver(rootPackageContext, projectEnvContext);
LinkedHashSet<ModuleLoadRequest> moduleLoadRequests = getModuleLoadRequestsOfDirectDependencies();
moduleResolver.resolveModuleLoadRequests(moduleLoadRequests);
this.dependencyGraph = cloneDependencyGraphNewRoot(packageResolution.dependencyGraph,
rootPackageContext.project().currentPackage());
this.dependencyGraphDump = packageResolution.dependencyGraphDump;
DependencyResolution dependencyResolution = new DependencyResolution(
projectEnvContext.getService(PackageCache.class), moduleResolver, dependencyGraph);
resolveDependencies(dependencyResolution);
}

private DependencyGraph<ResolvedPackageDependency> cloneDependencyGraphNewRoot
(DependencyGraph<ResolvedPackageDependency> depGraph, Package rootPackage) {
ResolvedPackageDependency oldRoot = depGraph.getRoot();
ResolvedPackageDependency newRoot = new ResolvedPackageDependency(rootPackage,
oldRoot.scope(), oldRoot.dependencyResolvedType());
DependencyGraphBuilder<ResolvedPackageDependency> depGraphBuilder =
DependencyGraphBuilder.getBuilder(newRoot);
for (ResolvedPackageDependency depNode : depGraph.getNodes()) {
if (depNode == oldRoot) {
depGraphBuilder.add(newRoot);
} else {
depGraphBuilder.add(depNode);
}
List<ResolvedPackageDependency> directPkgDependencies =
depGraph.getDirectDependencies(depNode)
.stream()
.map(directDepNode -> directDepNode == oldRoot ? newRoot : directDepNode)
.collect(Collectors.toList());
depGraphBuilder.addDependencies(depNode, directPkgDependencies);
}
return depGraphBuilder.build();
}

static PackageResolution from(PackageContext rootPackageContext, CompilationOptions compilationOptions) {
return new PackageResolution(rootPackageContext, compilationOptions);
}

static PackageResolution from(PackageResolution packageResolution, PackageContext
packageContext, CompilationOptions compilationOptions) {
return new PackageResolution(packageResolution, packageContext, compilationOptions);
}

/**
* Returns the package dependency graph of this package.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
import io.ballerina.projects.internal.model.BuildJson;
import io.ballerina.projects.internal.model.Dependency;
import io.ballerina.projects.internal.model.ToolDependency;
import io.ballerina.tools.diagnostics.Diagnostic;
import io.ballerina.tools.diagnostics.DiagnosticSeverity;
import org.apache.commons.compress.archivers.jar.JarArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntryPredicate;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
Expand Down Expand Up @@ -197,10 +199,18 @@ public static Set<String> getPackageImports(Package pkg) {
private static void getPackageImports(Set<String> imports, Module module, Collection<DocumentId> documentIds) {
for (DocumentId docId : documentIds) {
Document document = module.document(docId);

ModulePartNode modulePartNode = document.syntaxTree().rootNode();

for (ImportDeclarationNode importDcl : modulePartNode.imports()) {
boolean isErrorInImport = false;
for (Diagnostic diagnostic : importDcl.diagnostics()) {
if (diagnostic.diagnosticInfo().severity() == DiagnosticSeverity.ERROR) {
isErrorInImport = true;
break;
}
}
if (isErrorInImport) {
continue;
}
String orgName = "";
if (importDcl.orgName().isPresent()) {
orgName = importDcl.orgName().get().orgName().text();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,8 @@ private BLangStatementExpression createIfElseFromConfigurable(BLangSimpleVariabl
BLangStatementExpression stmtExpr = createStatementExpression(ifElse, resultVarRef);
stmtExpr.setBType(configurableVar.getBType());

return rewrite(stmtExpr, initFunctionEnv);
// This statement expression is desugared when it is visited in the init function
return stmtExpr;
}

private List<BLangExpression> getConfigurableLangLibInvocationParam(BLangSimpleVariable configurableVar) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5685,8 +5685,8 @@ private BErrorType createErrorType(BType lhsType, BType rhsType, BType detailTyp
BErrorType lhsErrorType = (BErrorType) lhsType;
BErrorType rhsErrorType = (BErrorType) rhsType;

BErrorType errorType = createErrorType(detailType, lhsType.flags, env);
errorType.tsymbol.flags |= rhsType.flags;
long flags = lhsType.flags | rhsType.flags | Flags.PUBLIC; // Anonymous (generated) types are marked as public.
BErrorType errorType = createErrorType(detailType, flags, env);

errorType.typeIdSet = BTypeIdSet.getIntersection(lhsErrorType.typeIdSet, rhsErrorType.typeIdSet);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,6 @@ salary = 25000.0
[[main.empInfoTab]]
id = 303
name = "belle"

[main.customConfig]
username = "chiranS"
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ type EmployeeInfo record {|
float salary?;
|};

public type CustomConfiguration record {
string username;
string logLevel = "OFF";
};

configurable CustomConfiguration customConfig = ?;

type UserTable table<AuthInfo> key(username);

type EmployeeTable table<Employee> key(id) & readonly;
Expand Down Expand Up @@ -193,6 +200,9 @@ function testRecordValues() {
test:assertEquals(34, empInfo.id);
test:assertEquals("test", empInfo.name);
test:assertEquals(75000.0, empInfo["salary"]);

test:assertEquals("chiranS", customConfig.username);
test:assertEquals("OFF", customConfig.logLevel);
}

function testTableValues() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,13 @@ public void testUnsupportedIntersectionNegative() {
assertEquals(result.getErrorCount(), index);
}

@Test
public void testErrorIntersectionAccessTest() {
CompileResult result = BCompileUtil.compile("test-src/types/intersection/error-intersection-access");
assertEquals(result.getErrorCount(), 0);
BRunUtil.invoke(result, "testErrorIntersectionFromImportedModule");
}

@AfterClass
public void tearDown() {
readOnlyIntersectionResults = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,22 @@ configurable UserInfo admin = {
password: "password"
};

public type CustomConfiguration record {
string username;
string logLevel = "OFF";
};

configurable CustomConfiguration customConfig1 = {username: "chiranS"};

configurable CustomConfiguration customConfig2 = {username: "chiranS", logLevel: "DEBUG"};

function testConfigValue() {
assertEquality("default", admin.username);
assertEquality("password", admin.password);
assertEquality("chiranS", customConfig1.username);
assertEquality("OFF", customConfig1.logLevel);
assertEquality("chiranS", customConfig2.username);
assertEquality("DEBUG", customConfig2.logLevel);
}

function assertEquality(any|error expected, any|error actual) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
org = "testorg"
name = "error_intersection_access"
version = "0.1.0"

[build-options]
observabilityIncluded = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
//
// WSO2 LLC. 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 error_intersection_access.error_intersection_mod as err_lib;

const MESSAGE = "Message";

public function testErrorIntersectionFromImportedModule() {
err_lib:RemoteServerError remoteServerErr = error err_lib:RemoteServerError(MESSAGE);
assertError(remoteServerErr);

err_lib:ApplicationResponseError applResponeErr = error err_lib:ApplicationResponseError(MESSAGE);
assertError(applResponeErr);

err_lib:ClientRequestErrorWithStatusCode clientReqErrWithStatusCode = error (MESSAGE, code = 404);
assertError(clientReqErrWithStatusCode, 404);

err_lib:ApplicationResponseErrorWithStatusCode appRespErrWithStatusCode = error (MESSAGE, code = 401);
assertError(appRespErrWithStatusCode, 401);

err_lib:BadRequestError badReqErr = error err_lib:BadRequestError(MESSAGE, code = 400);
assertError(badReqErr, 400);

err_lib:Error err = error err_lib:Error(MESSAGE);
assertError(err);
}

function assertError(any|error actual, int? code = ()) {
if actual !is error {
panic error(string `expected an error, found '${actual.toString()}'`);
}

if MESSAGE != actual.message() {
panic error(string `expected message: '${MESSAGE}', found: '${actual.message()}'`);
}

if code != () {
var detail = <record {|int code;|} & readonly> actual.detail();
if code != detail.code {
panic error(string `expected code: '${code}', found: '${detail.code}'`);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
//
// WSO2 LLC. 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.

public type Status record {
int code;
};

public type Error distinct error;

public type ClientRequestError distinct Error & error;

public type RemoteServerError distinct error & error;

public type ApplicationResponseError distinct ClientRequestError & RemoteServerError;

public type ApplicationResponseErrorWithStatusCode distinct ApplicationResponseError & error<Status>;

public type ClientRequestErrorWithStatusCode distinct ClientRequestError & error<Status>;

public type BadRequestError distinct ApplicationResponseErrorWithStatusCode & ClientRequestErrorWithStatusCode;
Loading