Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

ipfs.add + globSource + many files will freeze #2943

Closed
bluelovers opened this issue Mar 24, 2020 · 9 comments
Closed

ipfs.add + globSource + many files will freeze #2943

bluelovers opened this issue Mar 24, 2020 · 9 comments

Comments

@bluelovers
Copy link
Contributor

let ret = await addDirectoryToIPFS(ipfs, `target path`, {
			options: {
				pin:  false,
				//progress: createProgressBar,
			},
			globSourceOptions: {
				hidden: true,
			},
		});

		console.dir(ret.files.length);
		console.dir(ret.root.cid.toString());

use v1 will can't see any thing and freeze
use v2 will see some logs, but after many runs will freeze

both can't finish job

test file
https://ipfs.io/ipfs/QmQtUGvQnYv2ZDw5GdRsaMXqjsYGp3h74BwoFHRRSFiWRZ?filename=.cache.7z

v1

export async function addDirectoryToIPFS(ipfs: IIPFSFileApi, targetDirPath: string, {
	options,
	globSourceOptions,
}: {
	options?: IIPFSFileApiAddOptions,
	globSourceOptions?: IGlobSourceOptions,
} = {})
{
	let files: IIPFSFileApiAddReturnEntry[] = [];

	for await (const file of ipfs.add(globSource(targetDirPath, {
		recursive: true,
		...globSourceOptions,
	}), options))
	{
		console.log(file)
		files.push(file);
	}

	const root = files[files.length - 1]

	return {
		targetDirPath,
		root,
		files,
	}
}

v2

export async function addDirectoryToIPFS(ipfs: IIPFSFileApi, targetDirPath: string, {
	options,
	globSourceOptions,
}: {
	options?: IIPFSFileApiAddOptions,
	globSourceOptions?: IGlobSourceOptions,
} = {})
{

	const stream = globSource(targetDirPath, {
		recursive: true,
		...globSourceOptions,
	})

	let files: IIPFSFileApiAddReturnEntry[] = [];
	let root: IIPFSFileApiAddReturnEntry;

	let i = 0;

	// @ts-ignore
	for await (const file of ipfs.add(stream, options))
	{
		if ((i++ % 100) === 0)
		{
			console.dir(file.path)
			console.log(file.cid.toString());
			//console.dir(root = file)
		}

		root = file;
	}

	return {
		targetDirPath,
		root,
		files: {
			length: i,
		},
	}
}
@achingbrain
Copy link
Member

achingbrain commented Mar 24, 2020

Are you running this over http (e.g. to a daemon process) or direct to an in-process node?

@bluelovers
Copy link
Contributor Author

bluelovers commented Mar 24, 2020

over http and also try use direct by cli ( jsipfs add ) too

but all freeze

@achingbrain
Copy link
Member

Is the daemon running in the background when you use the CLI?

@bluelovers
Copy link
Contributor Author

bluelovers commented Mar 24, 2020

yes

image

@bluelovers
Copy link
Contributor Author

video
https://ipfs.io/ipfs/QmeaxfayZPFnhV8L6VpkfDYv2fTtN6BRsAYdXKHuGEZQZi?filename=bandicam%202020-03-25%2002-56-27-468.avi

first delay is by search files,

but............

after upload to 2x % is freezed, not more print any message

@achingbrain
Copy link
Member

achingbrain commented Mar 24, 2020

The next release will included reworked multipart requests and it should resolve this issue (hopefully later this week) - previously in order to use FormData requests we had to process every file that's being uploaded in advance as that's what the FormData API requires, but now we create a streaming multipart request.

This will work for node, but for the browser it will be fixed when they support streaming uploads - for more see #2838

@bluelovers
Copy link
Contributor Author

when i use v3 for upload, it still will freeze after upload many file

v3

export async function addDirectoryToIPFS(ipfs: IIPFSFilesApi & IIPFSFilesApi, targetDirPath: string, {
	options,
	globSourceOptions,
	ignoreExists,
}: {
	options?: IIPFSFileApiAddOptions,
	globSourceOptions?: IGlobSourceOptions,
	ignoreExists?: boolean,
} = {})
{

	const stream = globSource(targetDirPath, {
		recursive: true,
		...globSourceOptions,
	})

	let i = 0;
	let cid;

	ignoreExists = !!ignoreExists;

	for await (const entry of stream)
	{
		if (entry.content)
		{
			if (ignoreExists === true && await ipfsFilesExists(ipfs, entry.path))
			{
				console.gray.debug(entry.path)
				continue;
			}

			console.debug(entry.path)

			let buf = await getStream.buffer(entry.content)

			await ipfs.files.write(entry.path, buf, {
				create: true,
				parents: true,
				mode: entry.mode,
				mtime: entry.mtime,
			})

			i++;

			if ((i % 100) === 0)
			{
				const cid = await ipfs.files.flush()

				console.debug(cid.toString())
			}
		}
		else
		{
			console.debug(entry.path)
		}

	}

	cid = await ipfs.files.flush()

	return {
		targetDirPath,
		root: {
			cid,
		},
		files: {
			length: i,
		},
	}
}

@bluelovers
Copy link
Contributor Author

at v4 is same, it still freeze after update many files

so i think maybe is something happen in ipfs daemon

export async function addDirectoryToIPFS(ipfs: IIPFSFilesApi & IIPFSFilesApi, targetDirPath: string, {
	options,
	globSourceOptions,
	ignoreExists,
}: {
	options?: IIPFSFileApiAddOptions,
	globSourceOptions?: IGlobSourceOptions,
	ignoreExists?: boolean,
} = {})
{
	let i = 0;
	let cid;

	ignoreExists = !!ignoreExists;

	const rootPath = '/' + path.basename(targetDirPath) + '/';

	for await (let filename of FastGlob.stream([
		'**/*',
		'**/*.txt',
	], {
		cwd: targetDirPath,
		onlyFiles: true,
		//deep: Infinity,
	}))
	{
		filename = filename.toString();

		//console.dir(filename)

		let entry = {
			path: rootPath + filename,
			// @ts-ignore
			//content: createReadStream(path.join(targetDirPath, filename)),
			mode: undefined,
			mtime: undefined,
		}

		if (ignoreExists === true && await ipfsFilesExists(ipfs, entry.path))
		{
			console.gray.debug(entry.path)
			continue;
		}

		console.debug(entry.path)

		let buf = await readFile(path.join(targetDirPath, filename))

		await ipfs.files.write(entry.path, buf, {
			create: true,
			parents: true,
			mode: entry.mode,
			mtime: entry.mtime,
		})

		i++;

		if ((i % 100) === 0)
		{
			const cid = await ipfs.files.flush()

			console.success(cid.toString())
		}

	}

	cid = await ipfs.files.flush()

	return {
		targetDirPath,
		root: {
			cid,
		},
		files: {
			length: i,
		},
	}
}

@bluelovers
Copy link
Contributor Author

when it freeze

webui can't connect to daemon

image

and daemon is still live in console

image

@autonome autonome changed the title ipfs.add + globSouce + many files will freeze ipfs.add + globSource + many files will freeze Jul 17, 2020
SgtPooki referenced this issue in ipfs/js-kubo-rpc-client Aug 18, 2022
Adds a server running a gRPC endpoint over websockets running on port 5003, a `ipfs-grpc-client` module to access the server and a `ipfs-client` module that uses the gRPC client with HTTP fallback.

This is to solve shortcomings and limitations of the existing HTTP API and addresses the concerns raised in the 'Streaming HTTP APIs and errors, y u no work?' session we had at IPFS team week in NYC.

## Key points

1. Enables full duplex communication with a remote node

When making an HTTP request in the browser, a [FormData][] object must be created. In order to add all the values to the FormData object, an incoming stream must be consumed in its entirety before the first byte is sent to the server.

This means you cannot start processing a response before the request has been sent, so you cannot have full-duplex communication between client and server over HTTP. This seems unlikely to change in the near future.

With a websocket transport for gRPC-web, individual messages can be sent backwards and forwards by the client or the server enabling full-duplex communication.  This is essential for things like progress events from `ipfs.add` in the short term, and exposing the full stream capabilities of libp2p via remote client in the long term.

2. Enables streaming errors

The existing HTTP API sends errors as HTTP trailers.  No browser supports HTTP trailers so when a stream encounters an error, from the client's point of view the stream just stops with no possibility of finding out what happened.

This can also mask intended behaviour cause users to incorrectly interpret the API. For example if you specify a timeout to a DHT query and that timeout is reached, in the browser the stream ends without an error and you take away the results you've received thinking all is well but on the CLI the same operation results in a non-zero exit code.

A websocket transport has no restrictions here, since full-duplex communication is possible, errors can be received at any time.

3. Listens on websockets with no HTTP fallback

gRPC-web exists and is a way of exposing a gRPC service over HTTP.  Whereas gRPC supports four modes (unary, e.g. one request object and one response object, client streaming, server streaming and bidirectional streaming), gRPC-web only supports [unary and server streaming](https://github.com/grpc/grpc-web#wire-format-mode).  This is due to limitations of the web platform mentioned above and doesn't give us anything over our existing HTTP API.

The gRPC-web team are evaluating several options for client and bidirectional streaming, all of which require new capabilities to be added to browsers and none of which will be available in a reasonable time frame.

Notably they have [no plans to use websockets](https://github.com/grpc/grpc-web/blob/master/doc/streaming-roadmap.md#issues-with-websockets) as a transport, even though it solves the problems we have today.

The team from [improbable](https://improbable.io/) maintain a [gRPC-web-websockets bridge](https://github.com/improbable-eng/grpc-web) which the client added by this PR is compatible with.  Their bridge also has a go implementation of a [reverse proxy](https://github.com/improbable-eng/grpc-web/tree/master/go/grpcwebproxy) for use with gRPC servers to turn them into gRPC-web servers with an optional websocket transport.

My proposal is to embrace the use of websockets to solve our problems right now, then move to whatever streaming primitive the gRPC-web team settle on in the years to come.

As implemented there's only websockets here and no HTTP fallback as the existing HTTP API works fine for unary operations so there's little to be gained by blocking this work on reimplementing the whole of the HTTP API in gRPC-web, and the client can pick and choose which API it'll use per-call.

By running the websocket server on a different port to the existing HTTP API it gives us room to add gRPC-web fallback for the API if we find that useful.

4. Has protobuf definitions for all requests/responses

See the [ipfs-grpc-protocol](https://github.com/ipfs/js-ipfs/tree/feat/add-grpc-server-and-client/packages/ipfs-grpc-protocol) module, which contains definitions for API requests/reponses.

They've been ported from the existing API and will need some checking.

The [ipfs-grpc-server/README.md](https://github.com/ipfs/js-ipfs/blob/feat/add-grpc-server-and-client/packages/ipfs-grpc-server/README.md) has a rundown of the websocket communication protocol that was ported from [improbable-eng/grpc-web](https://github.com/improbable-eng/grpc-web).

5. Options as metadata

When making a request, metadata is sent during the preamble - these take the form of a string identical to HTTP headers as the initial websocket message - I've used this mechanism to send the options for a given invocation.

Notably these are not defined as a protocol buffer, just an unspecified list of simple key/value pairs - maybe they should be to ensure compatibility between implementations?

This will be trivial in the implementation in the PR as it contains a server implementation too but to do it in go will require patching or forking the improbable gRPC proxy.

6. Errors as metadata

Similar to the existing HTTP API, message trailers are used to send errors.  Four fields are used to re-construct the error on the client side:

| Field | Notes | 
| ----- | ----- |
| grpc-status  | 0 for success, 1+ for error |
| grpc-message | An error message |
| grpc-stack   | A stack trace with `\n` delimited lines |
| grpc-code    | A string code such as `'ERROR_BAD_INPUT'` that may be used for i18n translations to show a message to the user in their own language |

Similar to options these fields are unspecified, if a convention is not enough, perhaps they should be specified as a protobuf and the trailer sent as binary?

7. Streams

When sending data as part of an `ipfs.add`, we send repeated messages that contain a path, a content buffer and an index.  The index is used to differentiate between streams - path cannot be used as it could be empty.  Only the first supplied `path` is respected for a given index. On the server separate input streams are created for each file being added.  A file stream is considered closed when an unset or empty content buffer is received.  Ultimately this will allow us to apply backpressure on a per-file basis and read from different file streams in parallel and asymmetrically based on the available server capacity.

8. Performance

Observed performance pegs gRPC-web over websockets as similar to the HTTP Client with pretty much zero optimisation work performed

9. Security

Browsers require TLS for all use of websocket connections to localhost. They do not require it for the loopback address, however, which this PR uses, though loopback means the traffic will not leave the local machine.

The incoming requests start as HTTP requests so have a referer header and user agent so would follow the same restrictions as the existing HTTP API.

Fixes #2519
Fixes #2838
Fixes #2943
Fixes #2854
Fixes #2864

[FormData]: https://developer.mozilla.org/en-US/docs/Web/API/FormData
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants