Uploaded image for project: 'Spark'
  1. Spark
  2. SPARK-17535

Performance Improvement of Signleton pattern in SparkContext

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Major
    • Resolution: Won't Fix
    • 2.0.0
    • None
    • Spark Core
    • Important

    Description

      I think the singleton pattern of SparkContext is inefficient if there are many request to get the SparkContext,
      So we can write the singleton pattern as below,The second way if more efficient when there are many request to get the SparkContext.

       // the current version
        def getOrCreate(): SparkContext = {
          SPARK_CONTEXT_CONSTRUCTOR_LOCK.synchronized {
            if (activeContext.get() == null) {
              setActiveContext(new SparkContext(), allowMultipleContexts = false)
            }
            activeContext.get()
          }
        }
        // by myself
        def getOrCreate(): SparkContext = {
          if (activeContext.get() == null) {
            SPARK_CONTEXT_CONSTRUCTOR_LOCK.synchronized {
              if(activeContext.get == null) {
                @volatile val sparkContext = new SparkContext()
                setActiveContext(sparkContext, allowMultipleContexts = false)
              }
            }
            activeContext.get()
          }
        }
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            codlife Jianfei Wang
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: