# Buggy code
def tile(cls, op):
    if op.compression:
        return cls._tile_compressed(op)

    df = op.outputs[0]
    chunk_bytes = df.extra_params.chunk_bytes
    chunk_bytes = int(parse_readable_size(chunk_bytes)[0])

    dtypes = df.dtypes
    if (
        op.use_arrow_dtype is None and not op.gpu and options.dataframe.use_arrow_dtype
    ):  # pragma: no cover
        # check if use_arrow_dtype set on the server side
        dtypes = to_arrow_dtypes(df.dtypes)

    paths = (
        op.path
        if isinstance(op.path, (tuple, list))
        else glob(op.path, storage_options=op.storage_options)
    )

    out_chunks = []
    index_num = 0
    for path in paths:
        total_bytes = file_size(path)
        offset = 0
        for _ in range(int(np.ceil(total_bytes * 1.0 / chunk_bytes))):
            chunk_op = op.copy().reset_key()
            chunk_op._path = path
            chunk_op._offset = offset
            chunk_op._size = min(chunk_bytes, total_bytes - offset)
            shape = (np.nan, len(dtypes))
            index_value = parse_index(df.index_value.to_pandas(), path, index_num)
            new_chunk = chunk_op.new_chunk(
                None,
                shape=shape,
                index=(index_num, 0),
                index_value=index_value,
                columns_value=df.columns_value,
                dtypes=dtypes,
            )
            out_chunks.append(new_chunk)
            index_num += 1
            offset += chunk_bytes

    if (
        op.incremental_index
        and len(out_chunks) > 1
        and isinstance(df.index_value._index_value, IndexValue.RangeIndex)
    ):
        out_chunks = standardize_range_index(out_chunks)
    new_op = op.copy()
    nsplits = ((np.nan,) * len(out_chunks), (df.shape[1],))
    return new_op.new_dataframes(
        None,
        df.shape,
        dtypes=dtypes,
        index_value=df.index_value,
        columns_value=df.columns_value,
        chunks=out_chunks,
        nsplits=nsplits,
    )


# Traceback (OSError)
20/12/13 13:13:40 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
hdfsOpenFile(hdfs://<hdfs_ip>:8020/user/test/parquet_test): FileSystem#open((Lorg/apache/hadoop/fs/Path;I)Lorg/apache/hadoop/fs/FSDataInputStream;) error:
RemoteException: Path is not a file: /user/test/parquet_test
at org.apache.hadoop.hdfs.server.namenode.INodeFile.valueOf(INodeFile.java:70)
at org.apache.hadoop.hdfs.server.namenode.INodeFile.valueOf(INodeFile.java:56)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.getBlockLocationsInt(FSNamesystem.java:2092)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.getBlockLocations(FSNamesystem.java:2062)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.getBlockLocations(FSNamesystem.java:1975)
at org.apache.hadoop.hdfs.server.namenode.NameNodeRpcServer.getBlockLocations(NameNodeRpcServer.java:575)
at org.apache.hadoop.hdfs.server.namenode.AuthorizationProviderProxyClientProtocol.getBlockLocations(AuthorizationProviderProxyClientProtocol.java:92)
at org.apache.hadoop.hdfs.protocolPB.ClientNamenodeProtocolServerSideTranslatorPB.getBlockLocations(ClientNamenodeProtocolServerSideTranslatorPB.java:376)
at org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos$ClientNamenodeProtocol$2.callBlockingMethod(ClientNamenodeProtocolProtos.java)
at org.apache.hadoop.ipc.ProtobufRpcEngine$Server$ProtoBufRpcInvoker.call(ProtobufRpcEngine.java:617)
at org.apache.hadoop.ipc.RPC$Server.call(RPC.java:1073)
at org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:2226)
at org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:2222)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:422)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1917)
at org.apache.hadoop.ipc.Server$Handler.run(Server.java:2220)
java.io.FileNotFoundException: Path is not a file: /user/test/parquet_test
at org.apache.hadoop.hdfs.server.namenode.INodeFile.valueOf(INodeFile.java:70)
at org.apache.hadoop.hdfs.server.namenode.INodeFile.valueOf(INodeFile.java:56)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.getBlockLocationsInt(FSNamesystem.java:2092)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.getBlockLocations(FSNamesystem.java:2062)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.getBlockLocations(FSNamesystem.java:1975)
at org.apache.hadoop.hdfs.server.namenode.NameNodeRpcServer.getBlockLocations(NameNodeRpcServer.java:575)
at org.apache.hadoop.hdfs.server.namenode.AuthorizationProviderProxyClientProtocol.getBlockLocations(AuthorizationProviderProxyClientProtocol.java:92)
at org.apache.hadoop.hdfs.protocolPB.ClientNamenodeProtocolServerSideTranslatorPB.getBlockLocations(ClientNamenodeProtocolServerSideTranslatorPB.java:376)
at org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos$ClientNamenodeProtocol$2.callBlockingMethod(ClientNamenodeProtocolProtos.java)
at org.apache.hadoop.ipc.ProtobufRpcEngine$Server$ProtoBufRpcInvoker.call(ProtobufRpcEngine.java:617)
at org.apache.hadoop.ipc.RPC$Server.call(RPC.java:1073)
at org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:2226)
at org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:2222)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:422)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1917)
at org.apache.hadoop.ipc.Server$Handler.run(Server.java:2220)

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.apache.hadoop.ipc.RemoteException.instantiateException(RemoteException.java:106)
at org.apache.hadoop.ipc.RemoteException.unwrapRemoteException(RemoteException.java:73)
at org.apache.hadoop.hdfs.DFSClient.callGetBlockLocations(DFSClient.java:1289)
at org.apache.hadoop.hdfs.DFSClient.getLocatedBlocks(DFSClient.java:1274)
at org.apache.hadoop.hdfs.DFSClient.getLocatedBlocks(DFSClient.java:1262)
at org.apache.hadoop.hdfs.DFSInputStream.fetchLocatedBlocksAndGetLastBlockLength(DFSInputStream.java:307)
at org.apache.hadoop.hdfs.DFSInputStream.openInfo(DFSInputStream.java:273)
at org.apache.hadoop.hdfs.DFSInputStream.<init>(DFSInputStream.java:265)
at org.apache.hadoop.hdfs.DFSClient.open(DFSClient.java:1593)
at org.apache.hadoop.hdfs.DistributedFileSystem$4.doCall(DistributedFileSystem.java:338)
at org.apache.hadoop.hdfs.DistributedFileSystem$4.doCall(DistributedFileSystem.java:334)
at org.apache.hadoop.fs.FileSystemLinkResolver.resolve(FileSystemLinkResolver.java:81)
at org.apache.hadoop.hdfs.DistributedFileSystem.open(DistributedFileSystem.java:334)
Caused by: org.apache.hadoop.ipc.RemoteException(java.io.FileNotFoundException): Path is not a file: /user/test/parquet_test
at org.apache.hadoop.hdfs.server.namenode.INodeFile.valueOf(INodeFile.java:70)
at org.apache.hadoop.hdfs.server.namenode.INodeFile.valueOf(INodeFile.java:56)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.getBlockLocationsInt(FSNamesystem.java:2092)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.getBlockLocations(FSNamesystem.java:2062)
at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.getBlockLocations(FSNamesystem.java:1975)
at org.apache.hadoop.hdfs.server.namenode.NameNodeRpcServer.getBlockLocations(NameNodeRpcServer.java:575)
at org.apache.hadoop.hdfs.server.namenode.AuthorizationProviderProxyClientProtocol.getBlockLocations(AuthorizationProviderProxyClientProtocol.java:92)
at org.apache.hadoop.hdfs.protocolPB.ClientNamenodeProtocolServerSideTranslatorPB.getBlockLocations(ClientNamenodeProtocolServerSideTranslatorPB.java:376)
at org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos$ClientNamenodeProtocol$2.callBlockingMethod(ClientNamenodeProtocolProtos.java)
at org.apache.hadoop.ipc.ProtobufRpcEngine$Server$ProtoBufRpcInvoker.call(ProtobufRpcEngine.java:617)
at org.apache.hadoop.ipc.RPC$Server.call(RPC.java:1073)
at org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:2226)
at org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:2222)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:422)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1917)
at org.apache.hadoop.ipc.Server$Handler.run(Server.java:2220)

at org.apache.hadoop.ipc.Client.call(Client.java:1504)
at org.apache.hadoop.ipc.Client.call(Client.java:1441)
at org.apache.hadoop.ipc.ProtobufRpcEngine$Invoker.invoke(ProtobufRpcEngine.java:230)
at com.sun.proxy.$Proxy10.getBlockLocations(Unknown Source)
at org.apache.hadoop.hdfs.protocolPB.ClientNamenodeProtocolTranslatorPB.getBlockLocations(ClientNamenodeProtocolTranslatorPB.java:266)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.hadoop.io.retry.RetryInvocationHandler.invokeMethod(RetryInvocationHandler.java:260)
at org.apache.hadoop.io.retry.RetryInvocationHandler.invoke(RetryInvocationHandler.java:104)
at com.sun.proxy.$Proxy11.getBlockLocations(Unknown Source)
at org.apache.hadoop.hdfs.DFSClient.callGetBlockLocations(DFSClient.java:1287)
... 10 more
Traceback (most recent call last):
File "read_hdfs_dir.py", line 12, in <module>
df = md.read_parquet('hdfs://<hdfs_ip>:8020/user/test/parquet_test')
File "/home/test/lib/anaconda3/lib/python3.7/site-packages/mars/dataframe/datasource/read_parquet.py", line 394, in read_parquet
with open_file(file_path, storage_options=storage_options) as f:
File "/home/test/lib/anaconda3/lib/python3.7/site-packages/mars/filesystem.py", line 383, in open_file
f = fs.open(path, mode=mode)
File "pyarrow/io-hdfs.pxi", line 409, in pyarrow.lib.HadoopFileSystem.open
File "pyarrow/error.pxi", line 100, in pyarrow.lib.check_status
OSError: HDFS path exists, but opening file failed: hdfs://<hdfs_ip>:8020/user/test/parquet_test

# Fixed code
def tile(cls, op):
    if op.compression:
        return cls._tile_compressed(op)

    df = op.outputs[0]
    chunk_bytes = df.extra_params.chunk_bytes
    chunk_bytes = int(parse_readable_size(chunk_bytes)[0])

    dtypes = df.dtypes
    if (
        op.use_arrow_dtype is None and not op.gpu and options.dataframe.use_arrow_dtype
    ):  # pragma: no cover
        # check if use_arrow_dtype set on the server side
        dtypes = to_arrow_dtypes(df.dtypes)

    path_prefix = ""
    if isinstance(op.path, (tuple, list)):
        paths = op.path
    elif get_fs(op.path, op.storage_options).isdir(op.path):
        parsed_path = urlparse(op.path)
        if parsed_path.scheme.lower() == "hdfs":
            path_prefix = f"{parsed_path.scheme}://{parsed_path.netloc}"
            paths = get_fs(op.path, op.storage_options).ls(op.path)
        else:
            paths = glob(op.path.rstrip("/") + "/*", storage_options=op.storage_options)
    else:
        paths = glob(op.path, storage_options=op.storage_options)

    out_chunks = []
    index_num = 0
    for path in paths:
        path = path_prefix + path
        total_bytes = file_size(path)
        offset = 0
        for _ in range(int(np.ceil(total_bytes * 1.0 / chunk_bytes))):
            chunk_op = op.copy().reset_key()
            chunk_op._path = path
            chunk_op._offset = offset
            chunk_op._size = min(chunk_bytes, total_bytes - offset)
            shape = (np.nan, len(dtypes))
            index_value = parse_index(df.index_value.to_pandas(), path, index_num)
            new_chunk = chunk_op.new_chunk(
                None,
                shape=shape,
                index=(index_num, 0),
                index_value=index_value,
                columns_value=df.columns_value,
                dtypes=dtypes,
            )
            out_chunks.append(new_chunk)
            index_num += 1
            offset += chunk_bytes

    if (
        op.incremental_index
        and len(out_chunks) > 1
        and isinstance(df.index_value._index_value, IndexValue.RangeIndex)
    ):
        out_chunks = standardize_range_index(out_chunks)
    new_op = op.copy()
    nsplits = ((np.nan,) * len(out_chunks), (df.shape[1],))
    return new_op.new_dataframes(
        None,
        df.shape,
        dtypes=dtypes,
        index_value=df.index_value,
        columns_value=df.columns_value,
        chunks=out_chunks,
        nsplits=nsplits,
    )