#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#
# Unless required by applicable law or agreed to in writing, software
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
#

from __future__ import print_function

"""
.. note:: Experimental

"""

_attemptNumber = None
_partitionId = None
_stageId = None

def __new__(cls):
"""Even if users construct TaskContext instead of using get, give them the singleton."""

def __init__(self):
pass

@classmethod
def _getOrCreate(cls):
"""Internal function to get or create global TaskContext."""

[docs]    @classmethod
def get(cls):
"""
Return the currently active TaskContext. This can be called inside of

.. note:: Must be called on the worker, not the driver. Returns None if not initialized.
"""

[docs]    def stageId(self):
"""The ID of the stage that this task belong to."""
return self._stageId

[docs]    def partitionId(self):
"""
The ID of the RDD partition that is computed by this task.
"""
return self._partitionId

[docs]    def attemptNumber(self):
""""
How many times this task has been attempted.  The first task attempt will be assigned
attemptNumber = 0, and subsequent attempts will have increasing attempt numbers.
"""
return self._attemptNumber