This is an automated email from the ASF dual-hosted git repository.

aiceflower pushed a commit to branch release-0.9.4
in repository https://gitbox.apache.org/repos/asf/linkis.git

commit 3e0ecb8423dc834f2457cea3ac4f18bec824b826
Author: jftang <[email protected]>
AuthorDate: Wed Jun 3 22:45:03 2020 +0800

    add cs-persistence module
---
 contextservice/cs-persistence/pom.xml              |  87 ++++++++++
 .../cs/persistence/ContextPersistenceBeans.java    |  33 ++++
 .../cs/persistence/ContextPersistenceManager.java  |  24 +++
 .../persistence/ContextPersistenceManagerImpl.java |  89 ++++++++++
 .../linkis/cs/persistence/annotation/Ignore.java   |  14 ++
 .../linkis/cs/persistence/annotation/Tuning.java   |  14 ++
 .../persistence/aop/PersistenceTuningAspect.java   |  90 +++++++++++
 .../cs/persistence/conf/PersistenceConf.java       |  14 ++
 .../persistence/dao/ContextIDListenerMapper.java   |  21 +++
 .../linkis/cs/persistence/dao/ContextIDMapper.java |  16 ++
 .../persistence/dao/ContextKeyListenerMapper.java  |  22 +++
 .../cs/persistence/dao/ContextMapMapper.java       |  41 +++++
 .../dao/impl/contextIDListenerMapper.xml           |  47 ++++++
 .../cs/persistence/dao/impl/contextIDMapper.xml    |  61 +++++++
 .../dao/impl/contextKeyListenerMapper.xml          |  52 ++++++
 .../cs/persistence/dao/impl/contextMapMapper.xml   | 128 +++++++++++++++
 .../cs/persistence/entity/ExtraFieldClass.java     |  92 +++++++++++
 .../persistence/entity/PersistenceContextID.java   | 104 ++++++++++++
 .../entity/PersistenceContextIDListener.java       |  41 +++++
 .../persistence/entity/PersistenceContextKey.java  |  71 ++++++++
 .../entity/PersistenceContextKeyListener.java      |  41 +++++
 .../entity/PersistenceContextKeyValue.java         |  67 ++++++++
 .../entity/PersistenceContextValue.java            |  47 ++++++
 .../cs/persistence/exception/ThrowingFunction.java |  26 +++
 .../persistence/ContextIDListenerPersistence.java  |  25 +++
 .../persistence/ContextIDPersistence.java          |  20 +++
 .../persistence/ContextKeyListenerPersistence.java |  26 +++
 .../persistence/ContextMapPersistence.java         |  45 ++++++
 .../persistence/ContextMetricsPersistence.java     |   7 +
 .../persistence/TransactionManager.java            |  16 ++
 .../impl/ContextIDListenerPersistenceImpl.java     |  72 +++++++++
 .../persistence/impl/ContextIDPersistenceImpl.java |  78 +++++++++
 .../impl/ContextKeyListenerPersistenceImpl.java    |  98 +++++++++++
 .../impl/ContextMapPersistenceImpl.java            | 179 +++++++++++++++++++++
 .../impl/ContextMetricsPersistenceImpl.java        |  11 ++
 .../persistence/impl/TransactionManagerImpl.java   |  40 +++++
 .../cs/persistence/util/PersistenceUtils.java      | 144 +++++++++++++++++
 .../cs-persistence/src/main/resources/cs_ddl.sql   |  74 +++++++++
 38 files changed, 2077 insertions(+)

diff --git a/contextservice/cs-persistence/pom.xml 
b/contextservice/cs-persistence/pom.xml
new file mode 100644
index 0000000000..2895b19ec2
--- /dev/null
+++ b/contextservice/cs-persistence/pom.xml
@@ -0,0 +1,87 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Copyright 2019 WeBank
+  ~ Licensed 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.
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0";
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+    <parent>
+        <artifactId>linkis</artifactId>
+        <groupId>com.webank.wedatasphere.linkis</groupId>
+        <version>0.9.4</version>
+        <relativePath>../../pom.xml</relativePath>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>linkis-cs-persistence</artifactId>
+
+    <dependencies>
+        <dependency>
+            <groupId>com.webank.wedatasphere.linkis</groupId>
+            <artifactId>linkis-cs-common</artifactId>
+            <version>${linkis.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.webank.wedatasphere.linkis</groupId>
+            <artifactId>linkis-mybatis</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>com.webank.wedatasphere.linkis</groupId>
+            <artifactId>linkis-module</artifactId>
+            <version>${linkis.version}</version>
+            <scope>provided</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-math3</artifactId>
+            <version>3.1.1</version>
+        </dependency>
+
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.12</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-deploy-plugin</artifactId>
+            </plugin>
+
+            <plugin>
+                <groupId>net.alchim31.maven</groupId>
+                <artifactId>scala-maven-plugin</artifactId>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-jar-plugin</artifactId>
+            </plugin>
+        </plugins>
+        <resources>
+            <resource>
+                <directory>src/main/java</directory>
+                <includes>
+                    <include>**/*.xml</include>
+                </includes>
+            </resource>
+        </resources>
+        <finalName>${project.artifactId}-${project.version}</finalName>
+    </build>
+</project>
\ No newline at end of file
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/ContextPersistenceBeans.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/ContextPersistenceBeans.java
new file mode 100644
index 0000000000..2c35e9b532
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/ContextPersistenceBeans.java
@@ -0,0 +1,33 @@
+package com.webank.wedatasphere.linkis.cs.persistence;
+
+import com.webank.wedatasphere.linkis.cs.persistence.persistence.*;
+import 
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * Created by patinousward on 2020/2/23.
+ */
+@Configuration
+public class ContextPersistenceBeans {
+
+    @Bean
+    @ConditionalOnMissingBean(ContextPersistenceManager.class)
+    public ContextPersistenceManager getContextPersistenceManager(
+            ContextIDPersistence contextIDPersistence,
+            ContextMapPersistence contextMapPersistence,
+            ContextMetricsPersistence contextMetricsPersistence,
+            ContextIDListenerPersistence contextIDListenerPersistence,
+            ContextKeyListenerPersistence contextKeyListenerPersistence,
+            TransactionManager transactionManager
+    ) {
+        ContextPersistenceManagerImpl manager = new 
ContextPersistenceManagerImpl();
+        manager.setContextIDPersistence(contextIDPersistence);
+        manager.setContextMapPersistence(contextMapPersistence);
+        manager.setContextMetricsPersistence(contextMetricsPersistence);
+        
manager.setContextKeyListenerPersistence(contextKeyListenerPersistence);
+        manager.setContextIDListenerPersistence(contextIDListenerPersistence);
+        manager.setTransactionManager(transactionManager);
+        return manager;
+    }
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/ContextPersistenceManager.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/ContextPersistenceManager.java
new file mode 100644
index 0000000000..9024657cc6
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/ContextPersistenceManager.java
@@ -0,0 +1,24 @@
+package com.webank.wedatasphere.linkis.cs.persistence;
+
+import com.webank.wedatasphere.linkis.cs.persistence.persistence.*;
+
+/**
+ * Created by patinousward on 2020/2/11.
+ */
+public interface ContextPersistenceManager {
+
+    ContextIDPersistence getContextIDPersistence();
+
+    ContextMapPersistence getContextMapPersistence();
+
+
+
+    ContextMetricsPersistence getContextMetricsPersistence();
+
+    ContextIDListenerPersistence getContextIDListenerPersistence();
+
+    ContextKeyListenerPersistence getContextKeyListenerPersistence();
+
+    TransactionManager getTransactionManager();
+
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/ContextPersistenceManagerImpl.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/ContextPersistenceManagerImpl.java
new file mode 100644
index 0000000000..5f8eb58e9c
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/ContextPersistenceManagerImpl.java
@@ -0,0 +1,89 @@
+package com.webank.wedatasphere.linkis.cs.persistence;
+
+import com.webank.wedatasphere.linkis.cs.persistence.annotation.Tuning;
+import com.webank.wedatasphere.linkis.cs.persistence.persistence.*;
+
+/**
+ * Created by patinousward on 2020/2/17.
+ */
+public class ContextPersistenceManagerImpl implements 
ContextPersistenceManager {
+
+
+    private ContextIDPersistence contextIDPersistence;
+
+    private ContextMapPersistence contextMapPersistence;
+
+
+
+    private ContextMetricsPersistence contextMetricsPersistence;
+
+    private ContextIDListenerPersistence contextIDListenerPersistence;
+
+    private ContextKeyListenerPersistence contextKeyListenerPersistence;
+
+    private TransactionManager transactionManager;
+
+    @Override
+    @Tuning
+    public ContextIDPersistence getContextIDPersistence() {
+        return this.contextIDPersistence;
+    }
+
+    @Override
+    @Tuning
+    public ContextMapPersistence getContextMapPersistence() {
+        return this.contextMapPersistence;
+    }
+
+
+
+    @Override
+    @Tuning
+    public ContextMetricsPersistence getContextMetricsPersistence() {
+        return this.contextMetricsPersistence;
+    }
+
+    @Override
+    @Tuning
+    public ContextIDListenerPersistence getContextIDListenerPersistence() {
+        return this.contextIDListenerPersistence;
+    }
+
+    @Override
+    @Tuning
+    public ContextKeyListenerPersistence getContextKeyListenerPersistence() {
+        return this.contextKeyListenerPersistence;
+    }
+
+    @Override
+    public TransactionManager getTransactionManager() {
+        return this.transactionManager;
+    }
+
+    public void setContextIDPersistence(ContextIDPersistence 
contextIDPersistence) {
+        this.contextIDPersistence = contextIDPersistence;
+    }
+
+    public void setContextMapPersistence(ContextMapPersistence 
contextMapPersistence) {
+        this.contextMapPersistence = contextMapPersistence;
+    }
+
+
+    public void setContextMetricsPersistence(ContextMetricsPersistence 
contextMetricsPersistence) {
+        this.contextMetricsPersistence = contextMetricsPersistence;
+    }
+
+    public void setContextIDListenerPersistence(ContextIDListenerPersistence 
contextIDListenerPersistence) {
+        this.contextIDListenerPersistence = contextIDListenerPersistence;
+    }
+
+    public void setContextKeyListenerPersistence(ContextKeyListenerPersistence 
contextKeyListenerPersistence) {
+        this.contextKeyListenerPersistence = contextKeyListenerPersistence;
+    }
+
+    public void setTransactionManager(TransactionManager transactionManager) {
+        this.transactionManager = transactionManager;
+    }
+
+
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/annotation/Ignore.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/annotation/Ignore.java
new file mode 100644
index 0000000000..2d2f8923ce
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/annotation/Ignore.java
@@ -0,0 +1,14 @@
+package com.webank.wedatasphere.linkis.cs.persistence.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Created by patinousward on 2020/2/14.
+ */
+@Target({ElementType.TYPE, ElementType.FIELD})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Ignore {
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/annotation/Tuning.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/annotation/Tuning.java
new file mode 100644
index 0000000000..8be59d19a4
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/annotation/Tuning.java
@@ -0,0 +1,14 @@
+package com.webank.wedatasphere.linkis.cs.persistence.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Created by patinousward on 2020/2/23.
+ */
+@Target({ElementType.METHOD})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Tuning {
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/aop/PersistenceTuningAspect.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/aop/PersistenceTuningAspect.java
new file mode 100644
index 0000000000..6e6c6399b2
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/aop/PersistenceTuningAspect.java
@@ -0,0 +1,90 @@
+package com.webank.wedatasphere.linkis.cs.persistence.aop;
+
+import com.webank.wedatasphere.linkis.DataWorkCloudApplication;
+import com.webank.wedatasphere.linkis.cs.persistence.conf.PersistenceConf;
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.Signature;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Pointcut;
+import org.aspectj.lang.reflect.MethodSignature;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.NoSuchBeanDefinitionException;
+import org.springframework.context.ApplicationContext;
+import org.springframework.stereotype.Component;
+
+import java.lang.reflect.Method;
+
+/**
+ * Created by patinousward on 2020/2/23.
+ */
+@Aspect
+@Component
+public class PersistenceTuningAspect {
+
+    private Logger logger = LoggerFactory.getLogger(this.getClass());
+
+    private boolean tuningIsOpen = false;
+
+    private Object tuningObject = null;
+
+    private Method tuningMethod = null;
+
+    private boolean isInited = false;
+
+    public void init() {
+        synchronized (this) {
+            if (!isInited) {
+                try {
+                    Class<?> tuningClass = this.getClass().getClassLoader().
+                            loadClass(PersistenceConf.TUNING_CLASS.getValue());
+                    ApplicationContext context = 
DataWorkCloudApplication.getApplicationContext();
+                    if (context != null) {
+                        try {
+                            tuningObject = context.getBean(tuningClass);
+                            logger.info("find singleton tuning Object from 
IOC");
+                        } catch (NoSuchBeanDefinitionException e) {
+                            logger.info("can not find singleton  tuning Object 
from IOC");
+                        }
+                    }
+                    if (tuningObject == null) {
+                        tuningObject = tuningClass.newInstance();
+                    }
+                    tuningMethod = 
tuningClass.getMethod(PersistenceConf.TUNING_METHOD.getValue(), Object.class);
+                    tuningIsOpen = true;
+                } catch (ClassNotFoundException | InstantiationException |
+                        IllegalAccessException | NoSuchMethodException e) {
+                    logger.warn("can not load tuning class,tuning is close", 
e);
+                } finally {
+                    isInited = true;
+                }
+            }
+        }
+    }
+
+    @Pointcut(value = 
"@annotation(com.webank.wedatasphere.linkis.cs.persistence.annotation.Tuning)")
+    private void cut() {
+    }
+
+    @Around("cut()")
+    public Object around(ProceedingJoinPoint point) throws Throwable {
+        if (!isInited) {
+            init();
+        }
+        if (!tuningIsOpen) {
+            logger.info("tuning is close..return the real");
+            return point.proceed();
+        }
+        Signature signature = point.getSignature();
+        if (!(signature instanceof MethodSignature)) {
+            throw new IllegalArgumentException("该注解只能用于方法");
+        }
+        MethodSignature methodSignature = (MethodSignature) signature;
+        Object target = point.getTarget();
+        Method currentMethod = 
target.getClass().getMethod(methodSignature.getName(), 
methodSignature.getParameterTypes());
+        logger.info("调用方法:" + currentMethod.getName());
+        return tuningMethod.invoke(tuningObject, point.proceed());
+    }
+
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/conf/PersistenceConf.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/conf/PersistenceConf.java
new file mode 100644
index 0000000000..3246b248f6
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/conf/PersistenceConf.java
@@ -0,0 +1,14 @@
+package com.webank.wedatasphere.linkis.cs.persistence.conf;
+
+import com.webank.wedatasphere.linkis.common.conf.CommonVars;
+
+/**
+ * Created by patinousward on 2020/2/13.
+ */
+public class PersistenceConf {
+
+    public static final CommonVars<String> TUNING_CLASS = 
CommonVars.apply("wds.linkis.cs.ha.class", 
"com.webank.wedatasphere.linkis.cs.highavailable.DefaultContextHAManager");
+    //public static final CommonVars<String> TUNING_CLASS = 
CommonVars.apply("wds.linkis.cs.ha.class","com.webank.wedatasphere.linkis.cs.persistence.ProxyMethodA");
+
+    public static final CommonVars<String> TUNING_METHOD = 
CommonVars.apply("wds.linkis.cs.ha.proxymethod", "getContextHAProxy");
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/dao/ContextIDListenerMapper.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/dao/ContextIDListenerMapper.java
new file mode 100644
index 0000000000..5d89f5101f
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/dao/ContextIDListenerMapper.java
@@ -0,0 +1,21 @@
+package com.webank.wedatasphere.linkis.cs.persistence.dao;
+
+import com.webank.wedatasphere.linkis.cs.common.entity.source.ContextID;
+import 
com.webank.wedatasphere.linkis.cs.persistence.entity.PersistenceContextIDListener;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+
+/**
+ * Created by patinousward on 2020/2/17.
+ */
+public interface ContextIDListenerMapper {
+
+    void createIDListener(@Param("listener") PersistenceContextIDListener 
listener);
+
+    void remove(@Param("listener") PersistenceContextIDListener listener);
+
+    void removeAll(@Param("contextID") ContextID contextID);
+
+    List<PersistenceContextIDListener> getAll(@Param("contextID") ContextID 
contextID);
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/dao/ContextIDMapper.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/dao/ContextIDMapper.java
new file mode 100644
index 0000000000..da5b45cf7b
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/dao/ContextIDMapper.java
@@ -0,0 +1,16 @@
+package com.webank.wedatasphere.linkis.cs.persistence.dao;
+
+import 
com.webank.wedatasphere.linkis.cs.persistence.entity.PersistenceContextID;
+
+/**
+ * Created by patinousward on 2020/2/13.
+ */
+public interface ContextIDMapper {
+    void createContextID(PersistenceContextID persistenceContextID);
+
+    void deleteContextID(String contextId);
+
+    PersistenceContextID getContextID(String contextId);
+
+    void updateContextID(PersistenceContextID persistenceContextID);
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/dao/ContextKeyListenerMapper.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/dao/ContextKeyListenerMapper.java
new file mode 100644
index 0000000000..d14f7f25f4
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/dao/ContextKeyListenerMapper.java
@@ -0,0 +1,22 @@
+package com.webank.wedatasphere.linkis.cs.persistence.dao;
+
+import 
com.webank.wedatasphere.linkis.cs.common.entity.listener.ContextKeyListenerDomain;
+import com.webank.wedatasphere.linkis.cs.common.entity.source.ContextID;
+import 
com.webank.wedatasphere.linkis.cs.persistence.entity.PersistenceContextKeyListener;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+
+/**
+ * Created by patinousward on 2020/2/17.
+ */
+public interface ContextKeyListenerMapper {
+
+    void createKeyListener(@Param("listener") PersistenceContextKeyListener 
listener);
+
+    void remove(@Param("listener") ContextKeyListenerDomain 
contextKeyListenerDomain, @Param("keyId") Integer keyId);
+
+    void removeAll(@Param("keyIds") List<Integer> keyIds);
+
+    List<PersistenceContextKeyListener> getAll(@Param("keyIds") List<Integer> 
keyIds);
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/dao/ContextMapMapper.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/dao/ContextMapMapper.java
new file mode 100644
index 0000000000..fbd4174001
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/dao/ContextMapMapper.java
@@ -0,0 +1,41 @@
+package com.webank.wedatasphere.linkis.cs.persistence.dao;
+
+import 
com.webank.wedatasphere.linkis.cs.common.entity.enumeration.ContextScope;
+import com.webank.wedatasphere.linkis.cs.common.entity.enumeration.ContextType;
+import com.webank.wedatasphere.linkis.cs.common.entity.source.ContextID;
+import com.webank.wedatasphere.linkis.cs.common.entity.source.ContextKey;
+import 
com.webank.wedatasphere.linkis.cs.persistence.entity.PersistenceContextKeyValue;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+
+/**
+ * Created by patinousward on 2020/2/13.
+ */
+public interface ContextMapMapper {
+    void createMap(PersistenceContextKeyValue pKV);
+
+    void updateMap(PersistenceContextKeyValue pKV);
+
+    PersistenceContextKeyValue getContextMap(@Param("contextID") ContextID 
contextID, @Param("contextKey") ContextKey contextKey);
+
+    List<PersistenceContextKeyValue> getAllContextMapByKey(@Param("contextID") 
ContextID contextID, @Param("key") String key);
+
+    List<PersistenceContextKeyValue> 
getAllContextMapByContextID(@Param("contextID") ContextID contextID);
+
+    List<PersistenceContextKeyValue> 
getAllContextMapByScope(@Param("contextID") ContextID contextID, 
@Param("contextScope") ContextScope contextScope);
+
+    List<PersistenceContextKeyValue> 
getAllContextMapByType(@Param("contextID") ContextID contextID, 
@Param("contextType") ContextType contextType);
+
+    void removeContextMap(@Param("contextID") ContextID contextID, 
@Param("contextKey") ContextKey contextKey);
+
+    void removeAllContextMapByContextID(@Param("contextID") ContextID 
contextID);
+
+    void removeAllContextMapByType(@Param("contextID") ContextID contextID, 
@Param("contextType") ContextType contextType);
+
+    void removeAllContextMapByScope(@Param("contextID") ContextID contextID, 
@Param("contextScope") ContextScope contextScope);
+
+    void removeByKeyPrefixAndContextType(@Param("contextID") ContextID 
contextID, @Param("contextType") ContextType contextType, @Param("keyPrefix") 
String keyPrefix);
+
+    void removeByKeyPrefix(@Param("contextID") ContextID contextID, 
@Param("keyPrefix") String keyPrefix);
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/dao/impl/contextIDListenerMapper.xml
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/dao/impl/contextIDListenerMapper.xml
new file mode 100644
index 0000000000..f4a6fc7c03
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/dao/impl/contextIDListenerMapper.xml
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+  ~ Copyright 2019 WeBank
+  ~
+  ~ Licensed 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.
+  ~
+  -->
+
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"; >
+
+
+<mapper 
namespace="com.webank.wedatasphere.linkis.cs.persistence.dao.ContextIDListenerMapper">
+
+    <sql id="context_id_listener">
+        `id`,`listener_source`,`context_id`
+    </sql>
+
+    <insert id="createIDListener" useGeneratedKeys="true" keyProperty="id"
+            
parameterType="com.webank.wedatasphere.linkis.cs.persistence.entity.PersistenceContextIDListener">
+        INSERT INTO linkis_cs_context_listener(<include 
refid="context_id_listener"/>)
+        VALUES (#{listener.id},#{listener.source},#{listener.contextId})
+    </insert>
+
+    <delete id="remove">
+        DELETE FROM linkis_cs_context_listener where context_id = 
#{listener.contextId} AND listener_source = #{listener.source}
+    </delete>
+
+    <delete id="removeAll">
+        DELETE FROM linkis_cs_context_listener where context_id = 
#{contextID.contextId}
+    </delete>
+
+    <select id="getAll" 
resultType="com.webank.wedatasphere.linkis.cs.persistence.entity.PersistenceContextIDListener">
+        SELECT * FROM linkis_cs_context_listener where context_id = 
#{contextID.contextId}
+    </select>
+
+
+</mapper>
\ No newline at end of file
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/dao/impl/contextIDMapper.xml
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/dao/impl/contextIDMapper.xml
new file mode 100644
index 0000000000..1235e6da1c
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/dao/impl/contextIDMapper.xml
@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+  ~ Copyright 2019 WeBank
+  ~
+  ~ Licensed 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.
+  ~
+  -->
+
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"; >
+
+
+<mapper 
namespace="com.webank.wedatasphere.linkis.cs.persistence.dao.ContextIDMapper">
+
+    <sql id="context_id">
+        
`id`,`user`,`application`,`source`,`expire_type`,`expire_time`,`instance`,`backup_instance`
+    </sql>
+
+    <insert id="createContextID" useGeneratedKeys="true" 
keyProperty="contextId"
+            
parameterType="com.webank.wedatasphere.linkis.cs.persistence.entity.PersistenceContextID">
+        INSERT INTO linkis_cs_context_id(<include refid="context_id"/>)
+        VALUES 
(#{contextId},#{user},#{application},#{source},#{expireType},#{expireTime},#{instance},#{backupInstance})
+    </insert>
+
+    <delete id="deleteContextID">
+        DELETE FROM linkis_cs_context_id where id = #{contextId}
+    </delete>
+
+    <resultMap id="PersistenceContextID"
+               
type="com.webank.wedatasphere.linkis.cs.persistence.entity.PersistenceContextID">
+        <id column="id" property="contextId"/>
+    </resultMap>
+
+    <select id="getContextID" resultMap="PersistenceContextID">
+        SELECT * FROM linkis_cs_context_id where id = #{contextId}
+    </select>
+
+    <update id="updateContextID"
+            
parameterType="com.webank.wedatasphere.linkis.cs.persistence.entity.PersistenceContextID">
+        UPDATE linkis_cs_context_id
+        <trim prefix="set" suffixOverrides=",">
+            <if test="user != null">`user` = #{user},</if>
+            <if test="application != null">`application` = #{application},</if>
+            <if test="expireType != null">`expire_type` = #{expireType},</if>
+            <if test="expireTime != null">`expire_time` = #{expireTime},</if>
+            <if test="instance != null">`instance` = #{instance},</if>
+            <if test="backupInstance != null">`backup_instance` = 
#{backupInstance},</if>
+        </trim>
+        WHERE id = #{contextId}
+    </update>
+
+</mapper>
\ No newline at end of file
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/dao/impl/contextKeyListenerMapper.xml
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/dao/impl/contextKeyListenerMapper.xml
new file mode 100644
index 0000000000..094b0004c3
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/dao/impl/contextKeyListenerMapper.xml
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+  ~ Copyright 2019 WeBank
+  ~
+  ~ Licensed 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.
+  ~
+  -->
+
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"; >
+
+
+<mapper 
namespace="com.webank.wedatasphere.linkis.cs.persistence.dao.ContextKeyListenerMapper">
+
+    <sql id="context_key_listener">
+        `id`,`listener_source`,`key_id`
+    </sql>
+
+    <insert id="createKeyListener" useGeneratedKeys="true" keyProperty="id"
+            
parameterType="com.webank.wedatasphere.linkis.cs.persistence.entity.PersistenceContextKeyListener">
+        INSERT INTO linkis_cs_context_map_listener(<include 
refid="context_key_listener"/>)
+        VALUES (#{listener.id},#{listener.source},#{listener.keyId})
+    </insert>
+
+    <delete id="remove">
+        DELETE FROM linkis_cs_context_map_listener WHERE listener_source = 
#{listener.source} AND key_id = #{keyId}
+    </delete>
+
+    <delete id="removeAll">
+        DELETE FROM linkis_cs_context_map_listener WHERE
+        <foreach collection="keyIds" index="index" item="item" separator="," 
open="key_id in (" close=")">
+            #{item}
+        </foreach>
+    </delete>
+
+    <select id="getAll" 
resultType="com.webank.wedatasphere.linkis.cs.persistence.persistence.ContextKeyListenerPersistence">
+        SELECT * FROM linkis_cs_context_map_listener WHERE
+        <foreach collection="keyIds" item="keyid" index="i" open="key_id in (" 
close=")" separator=",">
+            #{keyid}
+        </foreach>
+    </select>
+
+</mapper>
\ No newline at end of file
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/dao/impl/contextMapMapper.xml
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/dao/impl/contextMapMapper.xml
new file mode 100644
index 0000000000..3c843b73fd
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/dao/impl/contextMapMapper.xml
@@ -0,0 +1,128 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+  ~ Copyright 2019 WeBank
+  ~
+  ~ Licensed 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.
+  ~
+  -->
+
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"; >
+
+
+<mapper 
namespace="com.webank.wedatasphere.linkis.cs.persistence.dao.ContextMapMapper">
+
+    <sql id="context_map">
+        
`id`,`key`,`context_scope`,`context_type`,`props`,`value`,`context_id`,`keywords`
+    </sql>
+
+    <insert id="createMap"
+            
parameterType="com.webank.wedatasphere.linkis.cs.persistence.entity.PersistenceContextKeyValue">
+        INSERT INTO linkis_cs_context_map(<include refid="context_map"/>)
+        VALUES 
(#{id},#{contextKey.key},#{contextKey.contextScope},#{contextKey.contextType},
+        
#{props},#{contextValue.valueStr},#{contextId},#{contextValue.keywords})
+    </insert>
+
+    <resultMap id="PersistenceContextKeyValue"
+               
type="com.webank.wedatasphere.linkis.cs.persistence.entity.PersistenceContextKeyValue">
+        <id column="id" property="id"></id>
+        <result column="context_id" property="contextId"></result>
+        <result column="props" property="props"></result>
+        <association property="contextKey"
+                     
javaType="com.webank.wedatasphere.linkis.cs.persistence.entity.PersistenceContextKey">
+            <result column="key" property="key"></result>
+            <result column="context_scope" property="contextScope"></result>
+            <result column="context_type" property="contextType"></result>
+            <result column="keywords" property="keywords"></result>
+        </association>
+        <association property="contextValue"
+                     
javaType="com.webank.wedatasphere.linkis.cs.persistence.entity.PersistenceContextValue">
+            <result column="value" property="valueStr"></result>
+        </association>
+    </resultMap>
+
+    <select id="getContextMap" resultMap="PersistenceContextKeyValue">
+        SELECT * FROM linkis_cs_context_map
+        <where>
+            <if test="contextID.contextId != null">context_id = 
#{contextID.contextId}</if>
+            <if test="contextKey.key != null">AND `key` = 
#{contextKey.key}</if>
+        </where>
+    </select>
+
+    <select id="getAllContextMapByKey" resultMap="PersistenceContextKeyValue">
+        SELECT * FROM linkis_cs_context_map WHERE context_id = 
#{contextID.contextId} AND `key` LIKE "%"#{key}"%"
+    </select>
+
+
+    <select id="getAllContextMapByContextID" 
resultMap="PersistenceContextKeyValue">
+        SELECT * FROM linkis_cs_context_map WHERE context_id = 
#{contextID.contextId}
+    </select>
+
+    <select id="getAllContextMapByScope" 
resultMap="PersistenceContextKeyValue">
+        SELECT * FROM linkis_cs_context_map WHERE context_id = 
#{contextID.contextId} AND context_scope = #{contextScope}
+    </select>
+
+    <select id="getAllContextMapByType" resultMap="PersistenceContextKeyValue">
+        SELECT * FROM linkis_cs_context_map WHERE context_id = 
#{contextID.contextId} AND context_type = #{contextType}
+    </select>
+
+    <delete id="removeContextMap">
+        DELETE FROM linkis_cs_context_map
+        <where>
+            <if test="contextID.contextId != null">context_id = 
#{contextID.contextId}</if>
+            <if test="contextKey.key != null">AND `key` = 
#{contextKey.key}</if>
+        </where>
+    </delete>
+
+    <delete id="removeAllContextMapByContextID">
+        DELETE FROM linkis_cs_context_map WHERE context_id = 
#{contextID.contextId}
+    </delete>
+
+    <delete id="removeAllContextMapByType">
+        DELETE FROM linkis_cs_context_map WHERE context_id = 
#{contextID.contextId} AND context_type = #{contextType}
+    </delete>
+
+    <delete id="removeAllContextMapByScope">
+        DELETE FROM linkis_cs_context_map WHERE context_id = 
#{contextID.contextId} AND context_scope = #{contextScope}
+    </delete>
+
+    <update id="updateMap"
+            
parameterType="com.webank.wedatasphere.linkis.cs.persistence.entity.PersistenceContextKeyValue">
+        UPDATE linkis_cs_context_map
+        <trim prefix="set" suffixOverrides=",">
+            <if test="contextKey.contextScope != null">`context_scope` = 
#{contextKey.contextScope},</if>
+            <if test="contextKey.contextType != null">`context_type` = 
#{contextKey.contextType},</if>
+            <if test="contextValue.valueStr != null">`value` = 
#{contextValue.valueStr},</if>
+            <if test="contextValue.keywords != null">`keywords` = 
#{contextValue.keywords},</if>
+        </trim>
+        WHERE context_id = #{contextId} AND `key` = #{contextKey.key}
+    </update>
+
+    <delete id="removeByKeyPrefix">
+        delete from linkis_cs_context_map
+        <where>
+            <if test="#{contextID.contextId} !=null">context_id = 
#{contextID.contextId} and </if>
+            <!--<if test="#{contextType} !=null">context_type = #{contextType} 
and </if>-->
+            <if test="#{keyPrefix} !=null">`key` like 
concat(#{keyPrefix},'%')</if>
+        </where>
+    </delete>
+    <!--todo 和上面的合并sql-->
+    <delete id="removeByKeyPrefixAndContextType">
+        delete from linkis_cs_context_map
+        <where>
+            <if test="#{contextID.contextId} !=null">context_id = 
#{contextID.contextId} and </if>
+            <if test="#{contextType} !=null">context_type = #{contextType} and 
</if>
+            <if test="#{keyPrefix} !=null">`key` like 
concat(#{keyPrefix},'%')</if>
+        </where>
+    </delete>
+
+</mapper>
\ No newline at end of file
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/entity/ExtraFieldClass.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/entity/ExtraFieldClass.java
new file mode 100644
index 0000000000..bca4b90549
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/entity/ExtraFieldClass.java
@@ -0,0 +1,92 @@
+package com.webank.wedatasphere.linkis.cs.persistence.entity;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Created by patinousward on 2020/2/14.
+ */
+public class ExtraFieldClass {
+
+    private String className;
+
+    private List<ExtraFieldClass> subs = new ArrayList<>();
+
+    private List<String> fieldNames = new ArrayList<>();
+
+    private List<Object> fieldValues = new ArrayList<>();
+
+    private List<String> fieldTypes = new ArrayList<>();
+
+    public void addSub(ExtraFieldClass sub) {
+        subs.add(sub);
+    }
+
+    public void addFieldName(String fieldName) {
+        fieldNames.add(fieldName);
+    }
+
+    public void addFieldValue(Object fieldValue) {
+        fieldValues.add(fieldValue);
+    }
+
+    public void addFieldType(String fieldtype) {
+        fieldTypes.add(fieldtype);
+    }
+
+    public ExtraFieldClass getOneSub(int index) {
+        return subs.get(index);
+    }
+
+    public String getOneFieldName(int index) {
+        return fieldNames.get(index);
+    }
+
+    public String getOneFieldType(int index) {
+        return fieldTypes.get(index);
+    }
+
+    public Object getOneFieldValue(int index) {
+        return fieldValues.get(index);
+    }
+
+    public String getClassName() {
+        return className;
+    }
+
+    public void setClassName(String className) {
+        this.className = className;
+    }
+
+    public List<ExtraFieldClass> getSubs() {
+        return subs;
+    }
+
+    public void setSubs(List<ExtraFieldClass> subs) {
+        this.subs = subs;
+    }
+
+    public List<String> getFieldNames() {
+        return fieldNames;
+    }
+
+    public void setFieldNames(List<String> fieldNames) {
+        this.fieldNames = fieldNames;
+    }
+
+    public List<Object> getFieldValues() {
+        return fieldValues;
+    }
+
+    public void setFieldValues(List<Object> fieldValues) {
+        this.fieldValues = fieldValues;
+    }
+
+    public List<String> getFieldTypes() {
+        return fieldTypes;
+    }
+
+    public void setFieldTypes(List<String> fieldTypes) {
+        this.fieldTypes = fieldTypes;
+    }
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/entity/PersistenceContextID.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/entity/PersistenceContextID.java
new file mode 100644
index 0000000000..04d517c6f3
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/entity/PersistenceContextID.java
@@ -0,0 +1,104 @@
+package com.webank.wedatasphere.linkis.cs.persistence.entity;
+
+import com.webank.wedatasphere.linkis.cs.common.entity.enumeration.ExpireType;
+import com.webank.wedatasphere.linkis.cs.common.entity.source.HAContextID;
+import com.webank.wedatasphere.linkis.cs.common.entity.source.UserContextID;
+import com.webank.wedatasphere.linkis.cs.persistence.annotation.Ignore;
+
+import java.util.Date;
+
+/**
+ * Created by patinousward on 2020/2/12.
+ */
+@Ignore
+public class PersistenceContextID implements UserContextID, HAContextID {
+
+    private String contextId;
+
+    private String user;
+
+    private String instance;
+
+    private String backupInstance;
+
+    private String application;
+
+    private ExpireType expireType;
+
+    private Date expireTime;
+
+    private String source;
+
+    public String getSource() {
+        return source;
+    }
+
+    public void setSource(String source) {
+        this.source = source;
+    }
+
+    @Override
+    public String getContextId() {
+        return this.contextId;
+    }
+
+    @Override
+    public void setContextId(String contextId) {
+        this.contextId = contextId;
+    }
+
+    @Override
+    public void setUser(String user) {
+        this.user = user;
+    }
+
+    @Override
+    public String getUser() {
+        return this.user;
+    }
+
+    @Override
+    public String getInstance() {
+        return this.instance;
+    }
+
+    @Override
+    public void setInstance(String instance) {
+        this.instance = instance;
+    }
+
+    @Override
+    public String getBackupInstance() {
+        return this.backupInstance;
+    }
+
+    @Override
+    public void setBackupInstance(String backupInstance) {
+        this.backupInstance = backupInstance;
+    }
+
+    public String getApplication() {
+        return application;
+    }
+
+    public void setApplication(String application) {
+        this.application = application;
+    }
+
+    public ExpireType getExpireType() {
+        return expireType;
+    }
+
+    public void setExpireType(ExpireType expireType) {
+        this.expireType = expireType;
+    }
+
+    public Date getExpireTime() {
+        return expireTime;
+    }
+
+    public void setExpireTime(Date expireTime) {
+        this.expireTime = expireTime;
+    }
+
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/entity/PersistenceContextIDListener.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/entity/PersistenceContextIDListener.java
new file mode 100644
index 0000000000..a777ec8d7f
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/entity/PersistenceContextIDListener.java
@@ -0,0 +1,41 @@
+package com.webank.wedatasphere.linkis.cs.persistence.entity;
+
+import com.webank.wedatasphere.linkis.cs.common.entity.listener.ListenerDomain;
+
+/**
+ * Created by patinousward on 2020/2/17.
+ */
+public class PersistenceContextIDListener implements ListenerDomain {
+
+    private Integer id;
+
+    private String source;
+
+    private String contextId;
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public String getContextId() {
+        return contextId;
+    }
+
+    public void setContextId(String contextId) {
+        this.contextId = contextId;
+    }
+
+    @Override
+    public String getSource() {
+        return this.source;
+    }
+
+    @Override
+    public void setSource(String source) {
+        this.source = source;
+    }
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/entity/PersistenceContextKey.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/entity/PersistenceContextKey.java
new file mode 100644
index 0000000000..f9ec7e5f28
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/entity/PersistenceContextKey.java
@@ -0,0 +1,71 @@
+package com.webank.wedatasphere.linkis.cs.persistence.entity;
+
+import 
com.webank.wedatasphere.linkis.cs.common.entity.enumeration.ContextScope;
+import com.webank.wedatasphere.linkis.cs.common.entity.enumeration.ContextType;
+import com.webank.wedatasphere.linkis.cs.common.entity.source.ContextKey;
+import com.webank.wedatasphere.linkis.cs.persistence.annotation.Ignore;
+
+/**
+ * Created by patinousward on 2020/2/12.
+ */
+@Ignore
+public class PersistenceContextKey implements ContextKey {
+
+    private String key;
+
+    private String keywords;
+
+    private ContextScope contextScope;
+
+    private ContextType contextType;
+
+    @Override
+    public String getKey() {
+        return this.key;
+    }
+
+    @Override
+    public void setKey(String key) {
+        this.key = key;
+    }
+
+    @Override
+    public ContextType getContextType() {
+        return this.contextType;
+    }
+
+    @Override
+    public void setContextType(ContextType contextType) {
+        this.contextType = contextType;
+    }
+
+    @Override
+    public ContextScope getContextScope() {
+        return this.contextScope;
+    }
+
+    @Override
+    public void setContextScope(ContextScope contextScope) {
+        this.contextScope = contextScope;
+    }
+
+    @Override
+    public String getKeywords() {
+        return this.keywords;
+    }
+
+    @Override
+    public void setKeywords(String keywords) {
+        this.keywords = keywords;
+    }
+
+    @Override
+    public int getType() {
+        return 0;
+    }
+
+    @Override
+    public void setType(int type) {
+
+    }
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/entity/PersistenceContextKeyListener.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/entity/PersistenceContextKeyListener.java
new file mode 100644
index 0000000000..5018e24fb7
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/entity/PersistenceContextKeyListener.java
@@ -0,0 +1,41 @@
+package com.webank.wedatasphere.linkis.cs.persistence.entity;
+
+import com.webank.wedatasphere.linkis.cs.common.entity.listener.ListenerDomain;
+
+/**
+ * Created by patinousward on 2020/2/17.
+ */
+public class PersistenceContextKeyListener implements ListenerDomain {
+
+    private Integer id;
+
+    private String source;
+
+    private Integer keyId;
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public Integer getKeyId() {
+        return keyId;
+    }
+
+    public void setKeyId(Integer keyId) {
+        this.keyId = keyId;
+    }
+
+    @Override
+    public String getSource() {
+        return this.source;
+    }
+
+    @Override
+    public void setSource(String source) {
+        this.source = source;
+    }
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/entity/PersistenceContextKeyValue.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/entity/PersistenceContextKeyValue.java
new file mode 100644
index 0000000000..3e86c45756
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/entity/PersistenceContextKeyValue.java
@@ -0,0 +1,67 @@
+package com.webank.wedatasphere.linkis.cs.persistence.entity;
+
+import com.webank.wedatasphere.linkis.cs.common.entity.source.ContextKey;
+import com.webank.wedatasphere.linkis.cs.common.entity.source.ContextKeyValue;
+import com.webank.wedatasphere.linkis.cs.common.entity.source.ContextValue;
+import com.webank.wedatasphere.linkis.cs.persistence.annotation.Ignore;
+
+/**
+ * Created by patinousward on 2020/2/12.
+ */
+@Ignore
+public class PersistenceContextKeyValue implements ContextKeyValue {
+
+    private Integer id;
+
+    private String contextId;
+
+    private ContextKey contextKey;
+
+    private ContextValue contextValue;
+
+    private String props;
+
+    public String getProps() {
+        return props;
+    }
+
+    public void setProps(String props) {
+        this.props = props;
+    }
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    @Override
+    public void setContextKey(ContextKey contextKey) {
+        this.contextKey = contextKey;
+    }
+
+    @Override
+    public ContextKey getContextKey() {
+        return this.contextKey;
+    }
+
+    @Override
+    public ContextValue getContextValue() {
+        return this.contextValue;
+    }
+
+    @Override
+    public void setContextValue(ContextValue contextValue) {
+        this.contextValue = contextValue;
+    }
+
+    public String getContextId() {
+        return contextId;
+    }
+
+    public void setContextId(String contextId) {
+        this.contextId = contextId;
+    }
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/entity/PersistenceContextValue.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/entity/PersistenceContextValue.java
new file mode 100644
index 0000000000..c8bc4136d3
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/entity/PersistenceContextValue.java
@@ -0,0 +1,47 @@
+package com.webank.wedatasphere.linkis.cs.persistence.entity;
+
+import com.webank.wedatasphere.linkis.cs.common.entity.source.ContextValue;
+import com.webank.wedatasphere.linkis.cs.persistence.annotation.Ignore;
+
+/**
+ * Created by patinousward on 2020/2/12.
+ */
+@Ignore
+public class PersistenceContextValue implements ContextValue {
+
+    private String keywords;
+    /**
+     * 序列化后的value
+     */
+    private Object value;
+
+    private String valueStr;
+
+    public String getValueStr() {
+        return valueStr;
+    }
+
+    public void setValueStr(String valueStr) {
+        this.valueStr = valueStr;
+    }
+
+    @Override
+    public String getKeywords() {
+        return this.keywords;
+    }
+
+    @Override
+    public void setKeywords(String keywords) {
+        this.keywords = keywords;
+    }
+
+    @Override
+    public Object getValue() {
+        return this.value;
+    }
+
+    @Override
+    public void setValue(Object value) {
+        this.value = value;
+    }
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/exception/ThrowingFunction.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/exception/ThrowingFunction.java
new file mode 100644
index 0000000000..a0396c3043
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/exception/ThrowingFunction.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright 2019 WeBank
+ *
+ * Licensed 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 com.webank.wedatasphere.linkis.cs.persistence.exception;
+
+/**
+ * Created by patinousward on 2019/11/13.
+ */
+@FunctionalInterface
+public interface ThrowingFunction<T, R, E extends Exception> {
+    R accept(T t) throws E;
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/ContextIDListenerPersistence.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/ContextIDListenerPersistence.java
new file mode 100644
index 0000000000..96c8d0472d
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/ContextIDListenerPersistence.java
@@ -0,0 +1,25 @@
+package com.webank.wedatasphere.linkis.cs.persistence.persistence;
+
+import 
com.webank.wedatasphere.linkis.cs.common.entity.listener.ContextIDListenerDomain;
+import com.webank.wedatasphere.linkis.cs.common.entity.source.ContextID;
+import com.webank.wedatasphere.linkis.cs.common.exception.CSErrorException;
+
+import java.util.List;
+
+/**
+ * Created by patinousward on 2020/2/11.
+ */
+public interface ContextIDListenerPersistence {
+
+    void create(ContextID contextID, ContextIDListenerDomain 
contextIDListenerDomain) throws CSErrorException;
+
+    void remove(ContextIDListenerDomain contextIDListenerDomain) throws 
CSErrorException;
+
+    void removeAll(ContextID contextID) throws CSErrorException;
+
+    List<ContextIDListenerDomain> getAll(ContextID contextID) throws 
CSErrorException;
+
+    ContextIDListenerDomain getBy(ContextIDListenerDomain 
contextIDListenerDomain) throws CSErrorException;
+
+
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/ContextIDPersistence.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/ContextIDPersistence.java
new file mode 100644
index 0000000000..f1ae02fb59
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/ContextIDPersistence.java
@@ -0,0 +1,20 @@
+package com.webank.wedatasphere.linkis.cs.persistence.persistence;
+
+import com.webank.wedatasphere.linkis.cs.common.entity.source.ContextID;
+import com.webank.wedatasphere.linkis.cs.common.exception.CSErrorException;
+import com.webank.wedatasphere.linkis.cs.common.listener.ContextIDListener;
+
+/**
+ * Created by patinousward on 2020/2/11.
+ */
+public interface ContextIDPersistence {
+
+    ContextID createContextID(ContextID contextID) throws CSErrorException;
+
+    void deleteContextID(String contextId) throws CSErrorException;
+
+    void updateContextID(ContextID contextID) throws CSErrorException;
+
+    ContextID getContextID(String contextId) throws CSErrorException;
+
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/ContextKeyListenerPersistence.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/ContextKeyListenerPersistence.java
new file mode 100644
index 0000000000..351d1d41a9
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/ContextKeyListenerPersistence.java
@@ -0,0 +1,26 @@
+package com.webank.wedatasphere.linkis.cs.persistence.persistence;
+
+import 
com.webank.wedatasphere.linkis.cs.common.entity.listener.ContextKeyListenerDomain;
+import com.webank.wedatasphere.linkis.cs.common.entity.source.ContextID;
+import com.webank.wedatasphere.linkis.cs.common.exception.CSErrorException;
+import com.webank.wedatasphere.linkis.cs.common.listener.ContextKeyListener;
+
+import java.util.List;
+
+/**
+ * Created by patinousward on 2020/2/11.
+ */
+public interface ContextKeyListenerPersistence {
+
+    void create(ContextID contextID, ContextKeyListenerDomain 
contextKeyListenerDomain) throws CSErrorException;
+
+    void remove(ContextID contextID, ContextKeyListenerDomain 
contextKeyListenerDomain) throws CSErrorException;
+
+    void removeAll(ContextID contextID) throws CSErrorException;
+
+    List<ContextKeyListenerDomain> getAll(ContextID contextID) throws 
CSErrorException;
+
+    ContextKeyListenerDomain getBy(ContextKeyListenerDomain 
contextKeyListenerDomain) throws CSErrorException;
+
+
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/ContextMapPersistence.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/ContextMapPersistence.java
new file mode 100644
index 0000000000..8a9c4496ac
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/ContextMapPersistence.java
@@ -0,0 +1,45 @@
+package com.webank.wedatasphere.linkis.cs.persistence.persistence;
+
+import 
com.webank.wedatasphere.linkis.cs.common.entity.enumeration.ContextScope;
+import com.webank.wedatasphere.linkis.cs.common.entity.enumeration.ContextType;
+import com.webank.wedatasphere.linkis.cs.common.entity.source.ContextID;
+import com.webank.wedatasphere.linkis.cs.common.entity.source.ContextKey;
+import com.webank.wedatasphere.linkis.cs.common.entity.source.ContextKeyValue;
+import com.webank.wedatasphere.linkis.cs.common.exception.CSErrorException;
+import com.webank.wedatasphere.linkis.cs.common.listener.ContextKeyListener;
+
+import java.util.List;
+
+/**
+ * Created by patinousward on 2020/2/11.
+ */
+public interface ContextMapPersistence {
+
+    void create(ContextID contextID, ContextKeyValue contextKeyValue) throws 
CSErrorException;
+
+    void update(ContextID contextID, ContextKeyValue contextKeyValue) throws 
CSErrorException;
+
+    ContextKeyValue get(ContextID contextID, ContextKey contextKey) throws 
CSErrorException;
+
+    List<ContextKeyValue> getAll(ContextID contextID, String key) throws 
CSErrorException;
+
+    List<ContextKeyValue> getAll(ContextID contextID) throws CSErrorException;
+
+    List<ContextKeyValue> getAll(ContextID contextID, ContextScope 
contextScope) throws CSErrorException;
+
+    List<ContextKeyValue> getAll(ContextID contextID, ContextType contextType) 
throws CSErrorException;
+
+    void reset(ContextID contextID, ContextKey contextKey) throws 
CSErrorException;
+
+    void remove(ContextID contextID, ContextKey contextKey) throws 
CSErrorException;
+
+    void removeAll(ContextID contextID) throws CSErrorException;
+
+    void removeAll(ContextID contextID, ContextType contextType) throws 
CSErrorException;
+
+    void removeAll(ContextID contextID, ContextScope contextScope) throws 
CSErrorException;
+
+    void removeByKeyPrefix(ContextID contextID, String keyPrefix);
+
+    void removeByKeyPrefix(ContextID contextID, ContextType contextType, 
String keyPrefix);
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/ContextMetricsPersistence.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/ContextMetricsPersistence.java
new file mode 100644
index 0000000000..8ec3928970
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/ContextMetricsPersistence.java
@@ -0,0 +1,7 @@
+package com.webank.wedatasphere.linkis.cs.persistence.persistence;
+
+/**
+ * Created by patinousward on 2020/2/11.
+ */
+public interface ContextMetricsPersistence {
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/TransactionManager.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/TransactionManager.java
new file mode 100644
index 0000000000..3a66e5158d
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/TransactionManager.java
@@ -0,0 +1,16 @@
+package com.webank.wedatasphere.linkis.cs.persistence.persistence;
+
+/**
+ * Created by patinousward on 2020/2/11.
+ */
+public interface TransactionManager {
+
+    Object begin();
+
+    void rollback(Object object);
+
+    void commit(Object object);
+
+    void onTransaction();
+
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/impl/ContextIDListenerPersistenceImpl.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/impl/ContextIDListenerPersistenceImpl.java
new file mode 100644
index 0000000000..87de119fbe
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/impl/ContextIDListenerPersistenceImpl.java
@@ -0,0 +1,72 @@
+package com.webank.wedatasphere.linkis.cs.persistence.persistence.impl;
+
+import 
com.webank.wedatasphere.linkis.cs.common.entity.listener.CommonContextIDListenerDomain;
+import 
com.webank.wedatasphere.linkis.cs.common.entity.listener.ContextIDListenerDomain;
+import com.webank.wedatasphere.linkis.cs.common.entity.source.ContextID;
+import com.webank.wedatasphere.linkis.cs.common.exception.CSErrorException;
+import 
com.webank.wedatasphere.linkis.cs.persistence.dao.ContextIDListenerMapper;
+import 
com.webank.wedatasphere.linkis.cs.persistence.entity.PersistenceContextIDListener;
+import 
com.webank.wedatasphere.linkis.cs.persistence.persistence.ContextIDListenerPersistence;
+import 
com.webank.wedatasphere.linkis.cs.persistence.persistence.ContextIDPersistence;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Created by patinousward on 2020/2/17.
+ */
+@Component
+public class ContextIDListenerPersistenceImpl implements 
ContextIDListenerPersistence {
+
+    @Autowired
+    private ContextIDListenerMapper contextIDListenerMapper;
+
+    @Autowired
+    private ContextIDPersistence contextIDPersistence;
+
+    @Override
+    public void create(ContextID contextID, ContextIDListenerDomain 
contextIDListenerDomain) throws CSErrorException {
+        PersistenceContextIDListener listener = new 
PersistenceContextIDListener();
+        listener.setContextId(contextID.getContextId());
+        listener.setSource(contextIDListenerDomain.getSource());
+        contextIDListenerMapper.createIDListener(listener);
+    }
+
+    @Override
+    public void remove(ContextIDListenerDomain contextIDListenerDomain) throws 
CSErrorException {
+        // TODO: 2020/2/17
+        PersistenceContextIDListener listener = new 
PersistenceContextIDListener();
+        
listener.setContextId(contextIDListenerDomain.getContextID().getContextId());
+        listener.setSource(contextIDListenerDomain.getSource());
+        contextIDListenerMapper.remove(listener);
+    }
+
+    @Override
+    public void removeAll(ContextID contextID) throws CSErrorException {
+        contextIDListenerMapper.removeAll(contextID);
+    }
+
+    @Override
+    public List<ContextIDListenerDomain> getAll(ContextID contextID) throws 
CSErrorException {
+        // 根据id返回一堆的domain
+        ContextID complete = 
contextIDPersistence.getContextID(contextID.getContextId());
+        List<PersistenceContextIDListener> listeners = 
contextIDListenerMapper.getAll(contextID);
+        List<ContextIDListenerDomain> domains = listeners.stream().map(l -> 
pDomainToCommon(l, complete)).collect(Collectors.toList());
+        return domains;
+    }
+
+    public ContextIDListenerDomain 
pDomainToCommon(PersistenceContextIDListener listener, ContextID contextID) {
+        CommonContextIDListenerDomain domain = new 
CommonContextIDListenerDomain();
+        domain.setContextID(contextID);
+        domain.setSource(listener.getSource());
+        return domain;
+    }
+
+    @Override
+    public ContextIDListenerDomain getBy(ContextIDListenerDomain 
contextIDListenerDomain) throws CSErrorException {
+        //根据id 和source 返回响应的ContextIDListenerDomain
+        return contextIDListenerDomain;
+    }
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/impl/ContextIDPersistenceImpl.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/impl/ContextIDPersistenceImpl.java
new file mode 100644
index 0000000000..7586d1d1d8
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/impl/ContextIDPersistenceImpl.java
@@ -0,0 +1,78 @@
+package com.webank.wedatasphere.linkis.cs.persistence.persistence.impl;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.webank.wedatasphere.linkis.cs.common.entity.source.ContextID;
+import com.webank.wedatasphere.linkis.cs.common.exception.CSErrorException;
+import com.webank.wedatasphere.linkis.cs.common.listener.ContextIDListener;
+import com.webank.wedatasphere.linkis.cs.persistence.dao.ContextIDMapper;
+import com.webank.wedatasphere.linkis.cs.persistence.entity.ExtraFieldClass;
+import 
com.webank.wedatasphere.linkis.cs.persistence.entity.PersistenceContextID;
+import 
com.webank.wedatasphere.linkis.cs.persistence.persistence.ContextIDPersistence;
+import com.webank.wedatasphere.linkis.cs.persistence.util.PersistenceUtils;
+import com.webank.wedatasphere.linkis.server.BDPJettyServerHelper;
+import org.apache.commons.math3.util.Pair;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.io.IOException;
+
+/**
+ * Created by patinousward on 2020/2/12.
+ */
+@Component
+public class ContextIDPersistenceImpl implements ContextIDPersistence {
+
+    private final Logger logger = LoggerFactory.getLogger(this.getClass());
+
+    @Autowired
+    private ContextIDMapper contextIDMapper;
+
+    private Class<PersistenceContextID> pClass = PersistenceContextID.class;
+
+    private ObjectMapper json = BDPJettyServerHelper.jacksonJson();
+
+    @Override
+    public ContextID createContextID(ContextID contextID) throws 
CSErrorException {
+        try {
+            Pair<PersistenceContextID, ExtraFieldClass> pContextID = 
PersistenceUtils.transfer(contextID, pClass);
+            
pContextID.getFirst().setSource(json.writeValueAsString(pContextID.getSecond()));
+            contextIDMapper.createContextID(pContextID.getFirst());
+            contextID.setContextId(pContextID.getFirst().getContextId());
+            return contextID;
+        } catch (JsonProcessingException e) {
+            logger.error("writeAsJson failed:", e);
+            throw new CSErrorException(97000, e.getMessage());
+        }
+    }
+
+    @Override
+    public void deleteContextID(String contextId) {
+        contextIDMapper.deleteContextID(contextId);
+    }
+
+    @Override
+    public void updateContextID(ContextID contextID) throws CSErrorException {
+        //contextId和source没有设置更新点
+        Pair<PersistenceContextID, ExtraFieldClass> pContextID = 
PersistenceUtils.transfer(contextID, pClass);
+        contextIDMapper.updateContextID(pContextID.getFirst());
+    }
+
+    @Override
+    public ContextID getContextID(String contextId) throws CSErrorException {
+        try {
+            PersistenceContextID pContextID = 
contextIDMapper.getContextID(contextId);
+            if(pContextID == null) return null;
+            ExtraFieldClass extraFieldClass = 
json.readValue(pContextID.getSource(), ExtraFieldClass.class);
+            ContextID contextID = PersistenceUtils.transfer(extraFieldClass, 
pContextID);
+            return contextID;
+        } catch (IOException e) {
+            logger.error("readJson failed:", e);
+            throw new CSErrorException(97000, e.getMessage());
+        }
+    }
+
+
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/impl/ContextKeyListenerPersistenceImpl.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/impl/ContextKeyListenerPersistenceImpl.java
new file mode 100644
index 0000000000..8415efc671
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/impl/ContextKeyListenerPersistenceImpl.java
@@ -0,0 +1,98 @@
+package com.webank.wedatasphere.linkis.cs.persistence.persistence.impl;
+
+import 
com.webank.wedatasphere.linkis.cs.common.entity.listener.CommonContextKeyListenerDomain;
+import 
com.webank.wedatasphere.linkis.cs.common.entity.listener.ContextKeyListenerDomain;
+import com.webank.wedatasphere.linkis.cs.common.entity.source.ContextID;
+import com.webank.wedatasphere.linkis.cs.common.entity.source.ContextKeyValue;
+import com.webank.wedatasphere.linkis.cs.common.exception.CSErrorException;
+import 
com.webank.wedatasphere.linkis.cs.persistence.dao.ContextKeyListenerMapper;
+import com.webank.wedatasphere.linkis.cs.persistence.dao.ContextMapMapper;
+import 
com.webank.wedatasphere.linkis.cs.persistence.entity.PersistenceContextKeyListener;
+import 
com.webank.wedatasphere.linkis.cs.persistence.entity.PersistenceContextKeyValue;
+import 
com.webank.wedatasphere.linkis.cs.persistence.persistence.ContextKeyListenerPersistence;
+import 
com.webank.wedatasphere.linkis.cs.persistence.persistence.ContextMapPersistence;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+/**
+ * Created by patinousward on 2020/2/17.
+ */
+@Component
+public class ContextKeyListenerPersistenceImpl implements 
ContextKeyListenerPersistence {
+
+    @Autowired
+    private ContextKeyListenerMapper contextKeyListenerMapper;
+
+    @Autowired
+    private ContextMapMapper contextMapMapper;
+
+    @Autowired
+    private ContextMapPersistence contextMapPersistence;
+
+    private final Logger logger = LoggerFactory.getLogger(this.getClass());
+
+    @Override
+    public void create(ContextID contextID, ContextKeyListenerDomain 
contextKeyListenerDomain) throws CSErrorException {
+        PersistenceContextKeyListener listener = new 
PersistenceContextKeyListener();
+        listener.setSource(contextKeyListenerDomain.getSource());
+        // contextKey 去数据库中查出响应的id
+        PersistenceContextKeyValue contextMap = 
contextMapMapper.getContextMap(contextID, 
contextKeyListenerDomain.getContextKey());
+        listener.setKeyId(contextMap.getId());
+        contextKeyListenerMapper.createKeyListener(listener);
+    }
+
+    @Override
+    public void remove(ContextID contextID, ContextKeyListenerDomain 
contextKeyListenerDomain) throws CSErrorException {
+        PersistenceContextKeyValue contextMap = 
contextMapMapper.getContextMap(contextID, 
contextKeyListenerDomain.getContextKey());
+        Integer keyId = contextMap.getId();
+        contextKeyListenerMapper.remove(contextKeyListenerDomain, keyId);
+    }
+
+    @Override
+    public void removeAll(ContextID contextID) throws CSErrorException {
+        //找到contextID 对应的所有map id
+        List<Integer> keyIds = 
contextMapMapper.getAllContextMapByContextID(contextID).stream()
+                
.map(PersistenceContextKeyValue::getId).collect(Collectors.toList());
+        contextKeyListenerMapper.removeAll(keyIds);
+    }
+
+    @Override
+    public List<ContextKeyListenerDomain> getAll(ContextID contextID) throws 
CSErrorException {
+        List<PersistenceContextKeyValue> pKVs = 
contextMapMapper.getAllContextMapByContextID(contextID);
+        List<Integer> keyIds = 
contextMapMapper.getAllContextMapByContextID(contextID).stream()
+                
.map(PersistenceContextKeyValue::getId).collect(Collectors.toList());
+        ArrayList<ContextKeyListenerDomain> domains = new ArrayList<>();
+        if (!keyIds.isEmpty()) {
+            logger.info("fetch %s keyIds by contextId %s", keyIds.size(), 
contextID.getContextId());
+            List<PersistenceContextKeyListener> listeners = 
contextKeyListenerMapper.getAll(keyIds);
+            for (PersistenceContextKeyListener listener : listeners) {
+                domains.add(pDomainToCommon(listener, contextID, pKVs));
+            }
+        }
+        return domains;
+    }
+
+    public ContextKeyListenerDomain 
pDomainToCommon(PersistenceContextKeyListener listener, ContextID contextID, 
List<PersistenceContextKeyValue> pKVs) throws CSErrorException {
+        CommonContextKeyListenerDomain domain = new 
CommonContextKeyListenerDomain();
+        domain.setSource(listener.getSource());
+        Optional<PersistenceContextKeyValue> first = pKVs.stream().filter(kv 
-> kv.getId().equals(listener.getId())).findFirst();
+        if (first.isPresent()) {
+            ContextKeyValue contextKeyValue = 
contextMapPersistence.get(contextID, first.get().getContextKey());
+            domain.setContextKey(contextKeyValue.getContextKey());
+        }
+        return domain;
+    }
+
+    @Override
+    public ContextKeyListenerDomain getBy(ContextKeyListenerDomain 
contextKeyListenerDomain) throws CSErrorException {
+
+        return contextKeyListenerDomain;
+    }
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/impl/ContextMapPersistenceImpl.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/impl/ContextMapPersistenceImpl.java
new file mode 100644
index 0000000000..5d3d7ed006
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/impl/ContextMapPersistenceImpl.java
@@ -0,0 +1,179 @@
+package com.webank.wedatasphere.linkis.cs.persistence.persistence.impl;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import 
com.webank.wedatasphere.linkis.cs.common.entity.enumeration.ContextScope;
+import com.webank.wedatasphere.linkis.cs.common.entity.enumeration.ContextType;
+import com.webank.wedatasphere.linkis.cs.common.entity.source.ContextID;
+import com.webank.wedatasphere.linkis.cs.common.entity.source.ContextKey;
+import com.webank.wedatasphere.linkis.cs.common.entity.source.ContextKeyValue;
+import com.webank.wedatasphere.linkis.cs.common.entity.source.ContextValue;
+import com.webank.wedatasphere.linkis.cs.common.exception.CSErrorException;
+import 
com.webank.wedatasphere.linkis.cs.common.serialize.helper.ContextSerializationHelper;
+import 
com.webank.wedatasphere.linkis.cs.common.serialize.helper.SerializationHelper;
+import com.webank.wedatasphere.linkis.cs.persistence.dao.ContextMapMapper;
+import com.webank.wedatasphere.linkis.cs.persistence.entity.ExtraFieldClass;
+import 
com.webank.wedatasphere.linkis.cs.persistence.entity.PersistenceContextKey;
+import 
com.webank.wedatasphere.linkis.cs.persistence.entity.PersistenceContextKeyValue;
+import 
com.webank.wedatasphere.linkis.cs.persistence.entity.PersistenceContextValue;
+import 
com.webank.wedatasphere.linkis.cs.persistence.persistence.ContextMapPersistence;
+import com.webank.wedatasphere.linkis.cs.persistence.util.PersistenceUtils;
+import com.webank.wedatasphere.linkis.server.BDPJettyServerHelper;
+import org.apache.commons.math3.util.Pair;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Created by patinousward on 2020/2/13.
+ */
+@Component
+public class ContextMapPersistenceImpl implements ContextMapPersistence {
+
+    @Autowired
+    private ContextMapMapper contextMapMapper;
+
+    private Class<PersistenceContextKey> pKClass = PersistenceContextKey.class;
+
+    private Class<PersistenceContextValue> pVClass = 
PersistenceContextValue.class;
+
+    private Class<PersistenceContextKeyValue> pKVClass = 
PersistenceContextKeyValue.class;
+
+    private final Logger logger = LoggerFactory.getLogger(this.getClass());
+
+    private ObjectMapper json = BDPJettyServerHelper.jacksonJson();
+
+    private final SerializationHelper serialHelper = 
ContextSerializationHelper.getInstance();
+
+    @Override
+    public void create(ContextID contextID, ContextKeyValue kV) throws 
CSErrorException {
+        // TODO: 2020/2/17 keywords 如何合并
+        try {
+            Pair<PersistenceContextKey, ExtraFieldClass> pK = 
PersistenceUtils.transfer(kV.getContextKey(), pKClass);
+            Pair<PersistenceContextValue, ExtraFieldClass> pV = 
PersistenceUtils.transfer(kV.getContextValue(), pVClass);
+            Pair<PersistenceContextKeyValue, ExtraFieldClass> pKV = 
PersistenceUtils.transfer(kV, pKVClass);
+            
pV.getFirst().setValueStr(serialHelper.serialize(kV.getContextValue().getValue()));
+            pKV.getSecond().addSub(pK.getSecond());
+            pKV.getSecond().addSub(pV.getSecond());
+            pKV.getFirst().setProps(json.writeValueAsString(pKV.getSecond()));
+            pKV.getFirst().setContextId(contextID.getContextId());
+            pKV.getFirst().setContextKey(pK.getFirst());
+            pKV.getFirst().setContextValue(pV.getFirst());
+            contextMapMapper.createMap(pKV.getFirst());
+        } catch (JsonProcessingException e) {
+            logger.error("writeAsJson failed:", e);
+            throw new CSErrorException(97000, e.getMessage());
+        }
+    }
+
+    @Override
+    public void update(ContextID contextID, ContextKeyValue kV) throws 
CSErrorException {
+        //根据contextId和key 进行更新
+        Pair<PersistenceContextKey, ExtraFieldClass> pK = 
PersistenceUtils.transfer(kV.getContextKey(), pKClass);
+        Pair<PersistenceContextValue, ExtraFieldClass> pV = 
PersistenceUtils.transfer(kV.getContextValue(), pVClass);
+        Pair<PersistenceContextKeyValue, ExtraFieldClass> pKV = 
PersistenceUtils.transfer(kV, pKVClass);
+        Object value = kV.getContextValue().getValue();
+        if (value != null) {
+            pV.getFirst().setValueStr(serialHelper.serialize(value));
+        }
+        pKV.getFirst().setContextKey(pK.getFirst());
+        pKV.getFirst().setContextValue(pV.getFirst());
+        pKV.getFirst().setContextId(contextID.getContextId());
+        contextMapMapper.updateMap(pKV.getFirst());
+    }
+
+    @Override
+    public ContextKeyValue get(ContextID contextID, ContextKey contextKey) 
throws CSErrorException {
+        //根据contextId 和key,获取到一个ContextKeyValue
+        PersistenceContextKeyValue pKV = 
contextMapMapper.getContextMap(contextID, contextKey);
+        if (pKV == null) return null;
+        return transfer(pKV);
+    }
+
+    private ContextKeyValue transfer(PersistenceContextKeyValue pKV) throws 
CSErrorException {
+        try {
+            // TODO: 2020/2/14 null return
+            PersistenceContextKey pK = (PersistenceContextKey) 
pKV.getContextKey();
+            PersistenceContextValue pV = (PersistenceContextValue) 
pKV.getContextValue();
+            ExtraFieldClass extraFieldClass = json.readValue(pKV.getProps(), 
ExtraFieldClass.class);
+            ContextKey key = 
PersistenceUtils.transfer(extraFieldClass.getOneSub(0), pK);
+            ContextValue value = 
PersistenceUtils.transfer(extraFieldClass.getOneSub(1), pV);
+            if(value != null){
+                value.setValue(serialHelper.deserialize(pV.getValueStr()));
+            }
+            ContextKeyValue kv = PersistenceUtils.transfer(extraFieldClass, 
pKV);
+            kv.setContextKey(key);
+            kv.setContextValue(value);
+            return kv;
+        } catch (IOException e) {
+            logger.error("readJson failed:", e);
+            throw new CSErrorException(97000, e.getMessage());
+        }
+    }
+
+    @Override
+    public List<ContextKeyValue> getAll(ContextID contextID, String key) {
+        //模糊匹配key
+        List<PersistenceContextKeyValue> pKVs = 
contextMapMapper.getAllContextMapByKey(contextID, key);
+        return 
pKVs.stream().map(PersistenceUtils.map(this::transfer)).collect(Collectors.toList());
+    }
+
+    @Override
+    public List<ContextKeyValue> getAll(ContextID contextID) {
+        List<PersistenceContextKeyValue> pKVs = 
contextMapMapper.getAllContextMapByContextID(contextID);
+        return 
pKVs.stream().map(PersistenceUtils.map(this::transfer)).collect(Collectors.toList());
+    }
+
+    @Override
+    public List<ContextKeyValue> getAll(ContextID contextID, ContextScope 
contextScope) {
+        List<PersistenceContextKeyValue> pKVs = 
contextMapMapper.getAllContextMapByScope(contextID, contextScope);
+        return 
pKVs.stream().map(PersistenceUtils.map(this::transfer)).collect(Collectors.toList());
+    }
+
+    @Override
+    public List<ContextKeyValue> getAll(ContextID contextID, ContextType 
contextType) {
+        List<PersistenceContextKeyValue> pKVs = 
contextMapMapper.getAllContextMapByType(contextID, contextType);
+        return 
pKVs.stream().map(PersistenceUtils.map(this::transfer)).collect(Collectors.toList());
+    }
+
+    @Override
+    public void reset(ContextID contextID, ContextKey contextKey) {
+
+    }
+
+    @Override
+    public void remove(ContextID contextID, ContextKey contextKey) {
+        contextMapMapper.removeContextMap(contextID, contextKey);
+    }
+
+    @Override
+    public void removeAll(ContextID contextID) {
+        contextMapMapper.removeAllContextMapByContextID(contextID);
+    }
+
+    @Override
+    public void removeAll(ContextID contextID, ContextType contextType) {
+        contextMapMapper.removeAllContextMapByType(contextID, contextType);
+    }
+
+    @Override
+    public void removeAll(ContextID contextID, ContextScope contextScope) {
+        contextMapMapper.removeAllContextMapByScope(contextID, contextScope);
+    }
+
+    @Override
+    public void removeByKeyPrefix(ContextID contextID, String keyPrefix) {
+        contextMapMapper.removeByKeyPrefix(contextID,keyPrefix);
+    }
+
+    @Override
+    public void removeByKeyPrefix(ContextID contextID, ContextType 
contextType, String keyPrefix) {
+        
contextMapMapper.removeByKeyPrefixAndContextType(contextID,contextType,keyPrefix);
+    }
+
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/impl/ContextMetricsPersistenceImpl.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/impl/ContextMetricsPersistenceImpl.java
new file mode 100644
index 0000000000..2697832c76
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/impl/ContextMetricsPersistenceImpl.java
@@ -0,0 +1,11 @@
+package com.webank.wedatasphere.linkis.cs.persistence.persistence.impl;
+
+import 
com.webank.wedatasphere.linkis.cs.persistence.persistence.ContextMetricsPersistence;
+import org.springframework.stereotype.Component;
+
+/**
+ * Created by patinousward on 2020/2/23.
+ */
+@Component
+public class ContextMetricsPersistenceImpl implements 
ContextMetricsPersistence {
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/impl/TransactionManagerImpl.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/impl/TransactionManagerImpl.java
new file mode 100644
index 0000000000..f6950396d0
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/persistence/impl/TransactionManagerImpl.java
@@ -0,0 +1,40 @@
+package com.webank.wedatasphere.linkis.cs.persistence.persistence.impl;
+
+import 
com.webank.wedatasphere.linkis.cs.persistence.persistence.TransactionManager;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.transaction.PlatformTransactionManager;
+import org.springframework.transaction.TransactionStatus;
+import org.springframework.transaction.support.DefaultTransactionDefinition;
+
+/**
+ * Created by patinousward on 2020/2/17.
+ */
+@Component
+public class TransactionManagerImpl implements TransactionManager {
+
+    @Autowired
+    private PlatformTransactionManager txManager;
+
+    @Override
+    public Object begin() {
+        return txManager.getTransaction(new DefaultTransactionDefinition());
+    }
+
+    @Override
+    public void rollback(Object object) {
+        TransactionStatus status = (TransactionStatus) object;
+        txManager.rollback(status);
+    }
+
+    @Override
+    public void commit(Object object) {
+        TransactionStatus status = (TransactionStatus) object;
+        txManager.commit(status);
+    }
+
+    @Override
+    public void onTransaction() {
+        // TODO: 2020/2/17  
+    }
+}
diff --git 
a/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/util/PersistenceUtils.java
 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/util/PersistenceUtils.java
new file mode 100644
index 0000000000..9bc1b96fc4
--- /dev/null
+++ 
b/contextservice/cs-persistence/src/main/java/com/webank/wedatasphere/linkis/cs/persistence/util/PersistenceUtils.java
@@ -0,0 +1,144 @@
+package com.webank.wedatasphere.linkis.cs.persistence.util;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.webank.wedatasphere.linkis.cs.common.exception.CSErrorException;
+import com.webank.wedatasphere.linkis.cs.common.exception.CSWarnException;
+import 
com.webank.wedatasphere.linkis.cs.common.serialize.helper.ContextSerializationHelper;
+import 
com.webank.wedatasphere.linkis.cs.common.serialize.helper.SerializationHelper;
+import com.webank.wedatasphere.linkis.cs.persistence.annotation.Ignore;
+import com.webank.wedatasphere.linkis.cs.persistence.entity.ExtraFieldClass;
+import 
com.webank.wedatasphere.linkis.cs.persistence.exception.ThrowingFunction;
+import com.webank.wedatasphere.linkis.server.BDPJettyServerHelper;
+import org.apache.commons.math3.util.Pair;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.BeanUtils;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.List;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+/**
+ * Created by patinousward on 2020/2/13.
+ */
+public class PersistenceUtils {
+
+    private static ObjectMapper json = BDPJettyServerHelper.jacksonJson();
+
+    private static final Logger logger = 
LoggerFactory.getLogger(PersistenceUtils.class);
+
+    private static String generateGetMethod(Field field) {
+        String fieldName = field.getName();
+        return String.format("get%s%s", fieldName.substring(0, 
1).toUpperCase(), fieldName.substring(1));
+    }
+
+    public static String generateSetMethod(String fieldName) {
+        return String.format("set%s%s", fieldName.substring(0, 
1).toUpperCase(), fieldName.substring(1));
+    }
+
+    private static boolean canIgnore(Field field) {
+        return field.getAnnotation(Ignore.class) != null;
+    }
+
+    private static List<String> getIgnoreFieldName(Class<?> clazz) {
+        if (clazz.getAnnotation(Ignore.class) != null) {
+            return 
Arrays.stream(clazz.getDeclaredFields()).map(Field::getName).collect(Collectors.toList());
+        } else {
+            return Arrays.stream(clazz.getDeclaredFields())
+                    
.filter(PersistenceUtils::canIgnore).map(Field::getName).collect(Collectors.toList());
+        }
+    }
+
+    public static <T, S> Pair<S, ExtraFieldClass> transfer(T t, Class<S> 
sClass) throws CSErrorException {
+        try {
+            ExtraFieldClass extraFieldClass = new ExtraFieldClass();
+            S s = sClass.newInstance();
+            BeanUtils.copyProperties(t, s);
+            Class<?> tClass = t.getClass();
+            extraFieldClass.setClassName(tClass.getName());
+            List<String> canIgnore = getIgnoreFieldName(sClass);
+            for (Field field : tClass.getDeclaredFields()) {
+                if (!canIgnore.contains(field.getName())) {
+                    Method method = tClass.getMethod(generateGetMethod(field));
+                    if (null != method.invoke(t)) {
+                        //field.getType().getName() 无法拿到子类的类型
+                        Object invoke = method.invoke(t);
+                        extraFieldClass.addFieldName(field.getName());
+                        if (invoke == null) {
+                            
extraFieldClass.addFieldType(field.getType().getName());
+                        } else {
+                            
extraFieldClass.addFieldType(invoke.getClass().getName());
+                        }
+                        extraFieldClass.addFieldValue(invoke);
+                    }
+                }
+            }
+            return new Pair<>(s, extraFieldClass);
+        } catch (Exception e) {
+            throw new CSErrorException(97000, "transfer bean failed:", e);
+        }
+    }
+
+    public static <T, S> T transfer(ExtraFieldClass extraFieldClass, S s) 
throws CSErrorException {
+        if (s == null) return null;
+        try {
+            Class<?> tClass = Class.forName(extraFieldClass.getClassName());
+            T t = (T) tClass.newInstance();
+            BeanUtils.copyProperties(s, t);
+            for (int i = 0; i < extraFieldClass.getFieldNames().size(); i++) {
+                Field field = 
tClass.getDeclaredField(extraFieldClass.getOneFieldName(i));
+                field.setAccessible(true);
+                if (LONG_TYP.equals(extraFieldClass.getOneFieldType(i))) {
+                    Long value = new 
Long(extraFieldClass.getOneFieldValue(i).toString());
+                    field.set(t, value);
+                } else if 
(Enum.class.isAssignableFrom(Class.forName(extraFieldClass.getOneFieldType(i))))
 {
+                    //反序列化支持枚举类
+                    Class<?> enumClass = 
Class.forName(extraFieldClass.getOneFieldType(i));
+                    Method valueOf = enumClass.getMethod("valueOf", 
String.class);
+                    Object invoke = valueOf.invoke(null, 
extraFieldClass.getOneFieldValue(i));
+                    field.set(t, invoke);
+                } else if 
(!BeanUtils.isSimpleProperty(Class.forName(extraFieldClass.getOneFieldType(i))))
 {
+                    //非基本类型的话,使用jackson进行反序列化  // TODO: 2020/3/5 
这里属性的序列化and反序列化最好修改为utils的序列化器
+                    Object o = 
json.convertValue(extraFieldClass.getOneFieldValue(i), 
Class.forName(extraFieldClass.getOneFieldType(i)));
+                    field.set(t, o);
+                } else {
+                    field.set(t, extraFieldClass.getOneFieldValue(i));
+                }
+            }
+            return t;
+        } catch (Exception e) {
+            throw new CSErrorException(97000, "transfer bean failed:", e);
+        }
+    }
+
+    public static <T, R, E extends Exception> Function<T, R> map(
+            ThrowingFunction<T, R, E> throwingFunction) {
+        return i -> {
+            try {
+                return throwingFunction.accept(i);
+            } catch (Exception e) {
+                throw new CSWarnException(97000, "execute failed,reason:", e);
+            }
+        };
+    }
+
+    private static final String LONG_TYP = "java.lang.Long";
+
+    // TODO: 2020/5/15  去掉重复的
+    public static final SerializationHelper SERIALIZE = 
ContextSerializationHelper.getInstance();
+
+    public static String serialize(Object o) throws CSErrorException {
+        if (o instanceof String) {
+            return (String) o;
+        }
+        return SERIALIZE.serialize(o);
+    }
+
+    public static <T> T deserialize(String Str) throws CSErrorException {
+        return (T) SERIALIZE.deserialize(Str);
+    }
+
+}
diff --git a/contextservice/cs-persistence/src/main/resources/cs_ddl.sql 
b/contextservice/cs-persistence/src/main/resources/cs_ddl.sql
new file mode 100644
index 0000000000..e09e1d98b5
--- /dev/null
+++ b/contextservice/cs-persistence/src/main/resources/cs_ddl.sql
@@ -0,0 +1,74 @@
+SET FOREIGN_KEY_CHECKS=0;
+
+-- ----------------------------
+-- Table structure for linkis_cs_context_history
+-- ----------------------------
+DROP TABLE IF EXISTS `linkis_cs_context_history`;
+CREATE TABLE `linkis_cs_context_history` (
+  `id` int(11) NOT NULL AUTO_INCREMENT,
+  `context_id` int(11) DEFAULT NULL,
+  `source` text,
+  `context_type` varchar(32) DEFAULT NULL,
+  `history_json` text,
+  `keyword` varchar(255) DEFAULT NULL,
+  PRIMARY KEY (`id`),
+  KEY `keyword` (`keyword`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+-- ----------------------------
+-- Table structure for linkis_cs_context_id
+-- ----------------------------
+DROP TABLE IF EXISTS `linkis_cs_context_id`;
+CREATE TABLE `linkis_cs_context_id` (
+  `id` int(11) NOT NULL AUTO_INCREMENT,
+  `user` varchar(32) DEFAULT NULL,
+  `application` varchar(32) DEFAULT NULL,
+  `source` varchar(255) DEFAULT NULL,
+  `expire_type` varchar(32) DEFAULT NULL,
+  `expire_time` datetime DEFAULT NULL,
+  `instance` varchar(32) DEFAULT NULL,
+  `backup_instance` varchar(255) DEFAULT NULL,
+  PRIMARY KEY (`id`),
+  KEY `instance` (`instance`),
+  KEY `backup_instance` (`backup_instance`),
+  KEY `instance_2` (`instance`,`backup_instance`)
+) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4;
+
+-- ----------------------------
+-- Table structure for linkis_cs_context_listener
+-- ----------------------------
+DROP TABLE IF EXISTS `linkis_cs_context_listener`;
+CREATE TABLE `linkis_cs_context_listener` (
+  `id` int(11) NOT NULL AUTO_INCREMENT,
+  `listener_source` varchar(255) DEFAULT NULL,
+  `context_id` int(11) DEFAULT NULL,
+  PRIMARY KEY (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+-- ----------------------------
+-- Table structure for linkis_cs_context_map
+-- ----------------------------
+DROP TABLE IF EXISTS `linkis_cs_context_map`;
+CREATE TABLE `linkis_cs_context_map` (
+  `id` int(11) NOT NULL AUTO_INCREMENT,
+  `key` varchar(255) DEFAULT NULL,
+  `context_scope` varchar(32) DEFAULT NULL,
+  `context_type` varchar(32) DEFAULT NULL,
+  `props` text,
+  `value` text,
+  `context_id` int(11) DEFAULT NULL,
+  `keywords` varchar(255) DEFAULT NULL,
+  PRIMARY KEY (`id`),
+  KEY `keywords` (`keywords`(191))
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
+
+-- ----------------------------
+-- Table structure for linkis_cs_context_map_listener
+-- ----------------------------
+DROP TABLE IF EXISTS `linkis_cs_context_map_listener`;
+CREATE TABLE `linkis_cs_context_map_listener` (
+  `id` int(11) NOT NULL AUTO_INCREMENT,
+  `listener_source` varchar(255) DEFAULT NULL,
+  `key_id` int(11) DEFAULT NULL,
+  PRIMARY KEY (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to