timoninmaxim commented on code in PR #12082: URL: https://github.com/apache/ignite/pull/12082#discussion_r2109664838
########## docs/_docs/code-snippets/java/src/main/java/org/apache/ignite/snippets/IgniteSessionContext.java: ########## @@ -0,0 +1,126 @@ +/* + * 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. + */ +package org.apache.ignite.snippets; + +import javax.cache.Cache; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.Map; + +import org.apache.ignite.cache.query.annotations.QuerySqlFunction; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.internal.util.typedef.F; +import org.apache.ignite.cache.CacheInterceptor; +import org.apache.ignite.lang.IgniteBiTuple; +import org.apache.ignite.resources.SessionContextProviderResource; +import org.apache.ignite.session.SessionContextProvider; +import org.jetbrains.annotations.Nullable; + +public class IgniteSessionContext { + + //tag::context[] + /** SessionContext interface. It's an entrypoint for all attributes. */ + public interface SessionContext { + + /** @return Attribute by name. */ + public @Nullable String getAttribute(String attrName); + } + //end::context[] + + //tag::sql-function[] + public class MyFunctions { + + @SessionContextProviderResource + public SessionContextProvider sesCtxProv; + + @QuerySqlFunction + public String sessionId() { + return sesCtxProv.getSessionContext().getAttribute("SESSION_ID"); + } + } + //end::sql-function[] + + //tag::cache-interceptor[] + public class SessionContextCacheInterceptor implements CacheInterceptor<Integer, String> { + /** */ + @SessionContextProviderResource + private SessionContextProvider sesCtxPrv; + + /** */ + @Override public @Nullable String onGet(Integer key, @Nullable String val) { + String ret = sesCtxPrv.getSessionContext().getAttribute("onGet"); + + return ret == null ? val : ret + key; + } + + /** */ + @Override public @Nullable String onBeforePut(Cache.Entry<Integer, String> entry, String newVal) { + String ret = sesCtxPrv.getSessionContext().getAttribute("onBeforePut"); + + return ret == null ? newVal : ret + entry.getKey(); + } + + /** */ + @Override public void onAfterPut(Cache.Entry<Integer, String> entry) { + + } + + /** */ + @Override public @Nullable IgniteBiTuple<Boolean, String> onBeforeRemove(Cache.Entry<Integer, String> entry) { + String ret = sesCtxPrv.getSessionContext().getAttribute("onBeforeRemove");; + + return new IgniteBiTuple<>(ret != null, entry.getValue()); + } + + /** */ + @Override public void onAfterRemove(Cache.Entry<Integer, String> entry) { + + } + } + //end::cache-interceptor[] + + public void igniteSessContext() { + //tag::ignite-context[] + IgniteConfiguration ignCfg = new IgniteConfiguration(); Review Comment: let's move this line before the tag. This is not a correct way to create IgniteConfiguration. Example just use ignCfg that user prepares somewhere ########## docs/_docs/session-context.adoc: ########## @@ -0,0 +1,69 @@ +// 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. += SessionContext API + +:javaFile: {javaCodeDir}/IgniteSessionContext.java + +== Overview + +Apache Ignite allows setting custom application attributes at runtime. These attributes may include session ID, application language, application name, etc. The attributes are set by the application only once for the Ignite API entry point (Ignite, JDBC) and are available on all participating nodes for application requests and queries. + +== SessionContext Interface + +`SessionContext` is an entry point for accessing attributes at all levels and other session-level data. Ignite provides `SessionContextProviderResource` annotation for access to SessionContext. User-defined functions, such as link:SQL/custom-sql-func[QuerySqlFunction] and `CacheInterceptor`, can access these attributes using `SessionContextProvider`. Review Comment: let's add link for CacheInterceptor docs similar to QuerySqlFunction ########## docs/_docs/code-snippets/java/src/main/java/org/apache/ignite/snippets/IgniteSessionContext.java: ########## @@ -0,0 +1,126 @@ +/* + * 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. + */ +package org.apache.ignite.snippets; + +import javax.cache.Cache; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.Map; + +import org.apache.ignite.cache.query.annotations.QuerySqlFunction; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.internal.util.typedef.F; +import org.apache.ignite.cache.CacheInterceptor; +import org.apache.ignite.lang.IgniteBiTuple; +import org.apache.ignite.resources.SessionContextProviderResource; +import org.apache.ignite.session.SessionContextProvider; +import org.jetbrains.annotations.Nullable; + +public class IgniteSessionContext { + + //tag::context[] + /** SessionContext interface. It's an entrypoint for all attributes. */ + public interface SessionContext { + + /** @return Attribute by name. */ + public @Nullable String getAttribute(String attrName); + } + //end::context[] + + //tag::sql-function[] + public class MyFunctions { + + @SessionContextProviderResource + public SessionContextProvider sesCtxProv; + + @QuerySqlFunction + public String sessionId() { + return sesCtxProv.getSessionContext().getAttribute("SESSION_ID"); + } + } + //end::sql-function[] + + //tag::cache-interceptor[] + public class SessionContextCacheInterceptor implements CacheInterceptor<Integer, String> { + /** */ + @SessionContextProviderResource + private SessionContextProvider sesCtxPrv; + + /** */ + @Override public @Nullable String onGet(Integer key, @Nullable String val) { + String ret = sesCtxPrv.getSessionContext().getAttribute("onGet"); + + return ret == null ? val : ret + key; + } + + /** */ + @Override public @Nullable String onBeforePut(Cache.Entry<Integer, String> entry, String newVal) { + String ret = sesCtxPrv.getSessionContext().getAttribute("onBeforePut"); + + return ret == null ? newVal : ret + entry.getKey(); + } + + /** */ + @Override public void onAfterPut(Cache.Entry<Integer, String> entry) { Review Comment: Let's remove onAfterPut, onAfterRemove methods as you don't use them in examples ########## docs/_docs/session-context.adoc: ########## @@ -0,0 +1,69 @@ +// 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. += SessionContext API + +:javaFile: {javaCodeDir}/IgniteSessionContext.java + +== Overview + +Apache Ignite allows setting custom application attributes at runtime. These attributes may include session ID, application language, application name, etc. The attributes are set by the application only once for the Ignite API entry point (Ignite, JDBC) and are available on all participating nodes for application requests and queries. + +== SessionContext Interface + +`SessionContext` is an entry point for accessing attributes at all levels and other session-level data. Ignite provides `SessionContextProviderResource` annotation for access to SessionContext. User-defined functions, such as link:SQL/custom-sql-func[QuerySqlFunction] and `CacheInterceptor`, can access these attributes using `SessionContextProvider`. + +[source, java] +---- +include::{javaFile}[tags=context,indent=0] +---- + +== Application Attributes in QuerySqlFunction [[sql-function]] + +User-defined SQL functions, like `QuerySqlFunction`, can utilize application attributes set on the client side: + +[source, java] +---- +include::{javaFile}[tags=sql-function,indent=0] +---- + +NOTE: Attribute access is only available in the link:SQL/sql-calcite[Calcite] query engine. In such cases, the class must have a public zero-argument constructor. + +== Application Attributes in CacheInterceptor + +You can configure a custom `CacheInterceptor` and write callbacks with access to application attributes, which are invoked before and after main cache operations: + +[source, java] +---- +include::{javaFile}[tags=cache-interceptor,indent=0] +---- + +== Setting application attributes + +=== Ignite + +You can set attributes using `Ignite.withApplicationAttributes(...)`. It returns an Ignite instance aware of the attributes. Application attributes are propagated with all messages sent within operations to remote nodes. Review Comment: "Application attributes are propagated to remote nodes." "with all messages" - it is an implementation detail, and actually isn't true. ########## docs/_docs/session-context.adoc: ########## @@ -0,0 +1,69 @@ +// 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. += SessionContext API + +:javaFile: {javaCodeDir}/IgniteSessionContext.java + +== Overview + +Apache Ignite allows setting custom application attributes at runtime. These attributes may include session ID, application language, application name, etc. The attributes are set by the application only once for the Ignite API entry point (Ignite, JDBC) and are available on all participating nodes for application requests and queries. + +== SessionContext Interface + +`SessionContext` is an entry point for accessing attributes at all levels and other session-level data. Ignite provides `SessionContextProviderResource` annotation for access to SessionContext. User-defined functions, such as link:SQL/custom-sql-func[QuerySqlFunction] and `CacheInterceptor`, can access these attributes using `SessionContextProvider`. + +[source, java] +---- +include::{javaFile}[tags=context,indent=0] +---- + +== Application Attributes in QuerySqlFunction [[sql-function]] + +User-defined SQL functions, like `QuerySqlFunction`, can utilize application attributes set on the client side: + +[source, java] +---- +include::{javaFile}[tags=sql-function,indent=0] +---- + +NOTE: Attribute access is only available in the link:SQL/sql-calcite[Calcite] query engine. In such cases, the class must have a public zero-argument constructor. + +== Application Attributes in CacheInterceptor + +You can configure a custom `CacheInterceptor` and write callbacks with access to application attributes, which are invoked before and after main cache operations: + +[source, java] +---- +include::{javaFile}[tags=cache-interceptor,indent=0] +---- + +== Setting application attributes + +=== Ignite + +You can set attributes using `Ignite.withApplicationAttributes(...)`. It returns an Ignite instance aware of the attributes. Application attributes are propagated with all messages sent within operations to remote nodes. + +[source, java] +---- +include::{javaFile}[tags=ignite-context,indent=0] +---- + +=== JDBC +The standard JDBC protocol provides the method `Connection.setClientInfo(...)` to set client attributes, which are available during the connection’s lifetime: Review Comment: let's mention that application attributes can be overridden with new call setClientInfo -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@ignite.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org